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_rsaStep 3: Add the SSH Key to Your GitHub Account
Copy the public key to your clipboard:
cat ~/.ssh/id_rsa.pubPaste it into your GitHub settings under SSH and GPG keys.
Step 4: Test Your Connection
ssh -T git@github.comEssential Git Commands
Cloning a Repository
git clone <repository-url>Checking the Status
git statusAdding Changes
git add .Committing Changes
git commit -m "Your commit message"Pushing Changes
git push origin mainPulling Changes
git pull origin mainChecking the Commit Log
git logCreating a New Branch
git checkout -b new-branchMerging Branches
git merge new-branchDeleting a Branch
git branch -d new-branchRetrieving a Previous Commit
To see the history of commits:
git log --onelineTo 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~1To undo the last commit and discard changes:
git reset --hard HEAD~1Final 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!