Introduction
Git is a widely used version control system that helps developers manage code changes efficiently. Setting up an SSH key is essential for secure authentication when interacting with remote repositories. In this article, we will cover the most important Git commands and how to set up your SSH key.
Setting Up an SSH Key
SSH keys provide a secure way to connect to your Git repositories without using a password.
Step 1: Generate an SSH Key
Run the following command to generate a new SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This command will generate a public and private key pair.
Step 2: Add Your SSH Key to the SSH Agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
Step 3: Add the SSH Key to Your GitHub Account
Copy the public key to your clipboard:
cat ~/.ssh/id_rsa.pub
Paste it into your GitHub settings under SSH and GPG keys.
Step 4: Test Your Connection
ssh -T git@github.com
Essential Git Commands
Cloning a Repository
git clone <repository-url>
Checking the Status
git status
Adding Changes
git add .
Committing Changes
git commit -m "Your commit message"
Pushing Changes
git push origin main
Pulling Changes
git pull origin main
Checking the Commit Log
git log
Creating a New Branch
git checkout -b new-branch
Merging Branches
git merge new-branch
Deleting a Branch
git branch -d new-branch
Retrieving a Previous Commit
To see the history of commits:
git log --oneline
To reset to a previous commit:
git reset --hard <commit-hash>
Stepping Back from a Commit
To undo the last commit but keep changes:
git reset --soft HEAD~1
To undo the last commit and discard changes:
git reset --hard HEAD~1
Final Thoughts
Mastering Git commands and setting up SSH keys ensures smooth version control and secure connections to repositories. Regularly using these commands will help you manage your projects efficiently. For more tips on Git usage, stay tuned!