Header Ads

Header ADS

Git setup with github

 

1. Configure Git with Your GitHub Username and Email

These settings will be used for every commit you make, so it's important to configure them with the same name and email you use on GitHub.

Open your terminal (or Git Bash if you are on Windows) and run the following commands:

git config --global user.name "Your GitHub Username"
git config --global user.email "your-email@example.com"

Replace "Your GitHub Username" and "your-email@example.com" with your actual GitHub username and the email associated with your GitHub account.

2. Generate SSH Key (Optional but Recommended)

Using SSH keys to authenticate with GitHub is more secure and convenient than using HTTPS every time you push changes.

a. Check for Existing SSH Keys

First, check if you already have an SSH key by running:

ls -al ~/.ssh

If you see files like id_rsa or id_ed25519, you already have an SSH key.

b. Generate a New SSH Key (if needed)

If you don't have an SSH key, generate one using:

ssh-keygen -t ed25519 -C "your-email@example.com"

If your system doesn't support ed25519, you can use rsa instead:

ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

When prompted, you can press Enter to save the key to the default location (~/.ssh/id_ed25519 or ~/.ssh/id_rsa).

c. Add the SSH Key to the SSH Agent

To ensure your SSH key is used correctly, start the SSH agent and add your key:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519  # or ~/.ssh/id_rsa if you used RSA

d. Add the SSH Key to Your GitHub Account

  1. Copy the SSH public key to your clipboard:

    cat ~/.ssh/id_ed25519.pub  # or id_rsa.pub
    

    Copy the output (it starts with ssh-ed25519).

  2. Go to your GitHub account.

  3. In the top-right corner, click your profile photo, then go to Settings.

  4. In the left sidebar, click SSH and GPG keys.

  5. Click the New SSH key button, give it a title, and paste your key in the field.

  6. Click Add SSH key.

3. Test the SSH Connection

You can verify your SSH connection to GitHub with this command:

ssh -T git@github.com

If successful, you’ll see a message like:

Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.

4. Clone a Repository from GitHub

Now, you can clone an existing GitHub repository to your local machine:

  1. Go to the repository on GitHub.

  2. Click the Code button and select SSH.

  3. Copy the URL (it should look like git@github.com:username/repository.git).

Then, run:

git clone git@github.com:username/repository.git

5. Create a New Repository and Push Code

If you want to create a new repository and push your code to GitHub:

a. Initialize a New Git Repository

Go to the folder where your project is, then initialize it as a Git repository:

git init

b. Add Files to the Repository

Add all your files to Git:

git add .

c. Commit Your Changes

Make your first commit:

git commit -m "Initial commit"

d. Create a New Repository on GitHub

Go to GitHub, and create a new repository. Don’t initialize it with a README or any files since you already have files in your local project.

e. Link Your Local Repository to GitHub

Once the repository is created, GitHub will show you the commands to link it. You need to add the remote URL (SSH) to your local Git repo:

git remote add origin git@github.com:username/repository.git

f. Push Your Code

Now, push your code to GitHub:

git push -u origin master  # or main, depending on your branch name

This will upload your code to the GitHub repository.

6. Using Git for Future Commits

For future changes, follow these steps:

  1. Check the status of your repository:

    git status
    
  2. Add changes to staging:

    git add .
    
  3. Commit the changes:

    git commit -m "Your commit message"
    
  4. Push changes to GitHub:

    git push
    

7. Pull Changes from GitHub (if collaborating)

If you're working with others and want to get the latest changes from GitHub, use:

git pull origin master  # or main, depending on the branch


Powered by Blogger.