This guide covers two common scenarios for working with Git repositories.
Before you start using Git, you need to configure your identity. Git uses this information to associate your commits with your identity.
These commands configure Git globally (across all repositories on your system):
# Set your Git username
git config --global user.name "Your Name"
# Set your Git email (use the email associated with your GitHub account)
git config --global user.email "your_email@example.com"To check your current Git configuration:
# View all global Git settings
git config --global --list
# View specific settings
git config --global user.name
git config --global user.email--globalapplies the configuration to all repositories on your computer- Without
--global, settings only apply to the current repository - Global settings are stored in
~/.gitconfig(orC:\Users\YourName\.gitconfigon Windows)
# Set default branch name to 'main'
git config --global init.defaultBranch main
# Set default editor (e.g., VS Code)
git config --global core.editor "code --wait"
# Enable colored output
git config --global color.ui auto
# Store credentials (for HTTPS)
git config --global credential.helper storeImportant: Always configure your username and email before making commits, as this information becomes part of your commit history and cannot be easily changed later.
Before you can push code to GitHub using SSH (the git@github.com: URLs shown in this guide), you need to set up SSH keys. SSH keys provide a secure way to authenticate with GitHub without entering your password each time.
SSH keys are recommended over HTTPS for Git operations because they:
- Provide secure authentication without passwords
- Allow seamless push/pull operations
- Are more secure than password authentication
First, check if you already have SSH keys:
# List existing SSH keys
ls -al ~/.sshLook for files named id_rsa.pub, id_ecdsa.pub, or id_ed25519.pub. If you see one of these, you can skip to Step 3.
If you don't have an SSH key, generate one:
# Generate a new SSH key (replace with your GitHub email)
ssh-keygen -t ed25519 -C "your_email@example.com"
# If your system doesn't support Ed25519, use RSA:
# ssh-keygen -t rsa -b 4096 -C "your_email@example.com"When prompted:
- Press Enter to accept the default file location (
~/.ssh/id_ed25519) - Enter a secure passphrase (optional but recommended)
- Confirm the passphrase
Start the SSH agent and add your key:
# Start the SSH agent in the background
eval "$(ssh-agent -s)" - LINUX
Start-Service ssh-agent - WINDOWS
# Add your SSH private key to the ssh-agent
ssh-add ~/.ssh/id_ed25519 - LINUX
ssh-add .ssh/id_ed25519 - WINDOWS
# If you used RSA, use:
# ssh-add ~/.ssh/id_rsa- Copy your SSH public key to the clipboard:
# Copy the SSH key to clipboard (Linux)
cat ~/.ssh/id_ed25519.pub
# On macOS, you can use:
# pbcopy < ~/.ssh/id_ed25519.pub
# On Windows (Git Bash), you can use:
clip < ~/.ssh/id_ed25519.pub - LINUX
Get-Content .ssh/id_ed25519.pub | clip - WINDOWS
- Go to GitHub and add the key:
- Navigate to Settings > SSH and GPG keys > New SSH key
- Give your key a descriptive title (e.g., "My Laptop")
- Paste your key into the "Key" field
- Click Add SSH key
Verify that your SSH key is working:
# Test SSH connection to GitHub
ssh -T git@github.comYou should see a message like: Hi username! You've successfully authenticated, but GitHub does not provide shell access.
For more detailed information, see the official GitHub documentation:
Use this approach when you're starting a project from scratch locally:
# Create a README file
echo "# TESTING" >> README.md
# Initialize a new Git repository
git init
# Stage the README file
git add README.md
# Create your first commit
git commit -m "first commit"
# Rename the default branch to 'main'
git branch -M main
# Add remote repository (skip if repository was created on GitHub)
# Use this command only when the repository was created locally
git remote add origin git@github.com:nbpacio/TESTING.git
# Push your commits to the remote repository
git push -u origin mainNote: The git remote add origin command should be skipped if you created the repository on GitHub first. Use it only when you've created the repository locally and need to connect it to a GitHub repository.
Use this approach when you already have a local repository with commits:
# Add the remote repository
git remote add origin git@github.com:nbpacio/TESTING.git
# Rename the current branch to 'main'
git branch -M main
# Push your commits to the remote repository
git push -u origin maingit init- Initializes a new Git repository in the current directorygit add- Stages files for commitgit commit -m "message"- Creates a commit with the staged changesgit branch -M main- Renames the current branch to 'main'git remote add origin <url>- Connects your local repository to a remote repositorygit push -u origin main- Pushes your commits to the remote repository and sets up tracking
- Approach A: Starting a brand new project locally
- Approach B: You have an existing local repository that needs to be pushed towards GitHub
Assuming you have made commits on your local main branch and wish to integrate them into the master branch on the remote repository, follow these steps:
git checkout master
Pull the latest changes from the remote master branch: This ensures your local master is up-to-date before merging.
git pull origin master
git merge main
This command integrates the changes from your local main branch into your local master branch. Resolve any merge conflicts that may arise during this step.
Push the updated master branch to the remote repository:
git push origin master