DEV Community

Cover image for Essential Git Commands Every Developer Wish To Know
Amit Chandra
Amit Chandra

Posted on

Essential Git Commands Every Developer Wish To Know

Git is an essential tool for every developer, enabling efficient version control and collaboration. Here’s a list of the most important Git commands you should master.

1. Git Configuration

Before you start using Git, configure your username and email:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode

Check your configuration with:

git config --list
Enter fullscreen mode Exit fullscreen mode

2. Initializing and Cloning Repositories

To start a new Git repository:

git init
Enter fullscreen mode Exit fullscreen mode

To clone an existing repository:

git clone https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode

3. Staging and Committing Changes

Check the status of your repository:

git status
Enter fullscreen mode Exit fullscreen mode

Stage files for commit:

git add filename    # Add a specific file
git add .           # Add all changes
Enter fullscreen mode Exit fullscreen mode

Commit changes:

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

4. Branching and Merging

Create a new branch:

git branch new-branch
Enter fullscreen mode Exit fullscreen mode

Switch to the new branch:

git checkout new-branch
Enter fullscreen mode Exit fullscreen mode

Alternatively, create and switch in one command:

git checkout -b new-branch
Enter fullscreen mode Exit fullscreen mode

Merge a branch into the main branch:

git checkout main
git merge new-branch
Enter fullscreen mode Exit fullscreen mode

Delete a branch:

git branch -d new-branch
Enter fullscreen mode Exit fullscreen mode

5. Working with Remote Repositories

To add a remote repository:

git remote add origin https://github.com/user/repository.git
Enter fullscreen mode Exit fullscreen mode

Push changes to a remote repository:

git push origin branch-name
Enter fullscreen mode Exit fullscreen mode

Fetch changes from a remote repository:

git fetch origin
Enter fullscreen mode Exit fullscreen mode

Pull changes and merge:

git pull origin branch-name
Enter fullscreen mode Exit fullscreen mode

6. Undoing Changes

To undo changes in a file before staging:

git checkout -- filename
Enter fullscreen mode Exit fullscreen mode

To remove a file from staging:

git reset filename
Enter fullscreen mode Exit fullscreen mode

To undo the last commit (without losing changes):

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

To undo the last commit (discard changes):

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

7. Checking History and Logs

View commit history:

git log
Enter fullscreen mode Exit fullscreen mode

View commit history in a single line:

git log --oneline --graph --decorate
Enter fullscreen mode Exit fullscreen mode

Check the last commit:

git show HEAD
Enter fullscreen mode Exit fullscreen mode

8. Stashing Changes

If you need to save changes temporarily without committing:

git stash
Enter fullscreen mode Exit fullscreen mode

To apply stashed changes:

git stash pop
Enter fullscreen mode Exit fullscreen mode

To list all stashes:

git stash list
Enter fullscreen mode Exit fullscreen mode

9. Resolving Merge Conflicts

When merging branches, you may encounter conflicts. Use:

git status
Enter fullscreen mode Exit fullscreen mode

Edit the conflicting files, then add and commit them:

git add .
git commit -m "Resolved merge conflict"
Enter fullscreen mode Exit fullscreen mode

10. Deleting Files and Commits

To remove a file and commit the deletion:

git rm filename
git commit -m "Deleted filename"
Enter fullscreen mode Exit fullscreen mode

To delete the last commit:

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

11. Amending the Last Commit (Modify Commit Without Creating a New One)

git commit --amend -m "Updated commit message"
Enter fullscreen mode Exit fullscreen mode

✅ Use this when you need to change the last commit’s message or include new changes without creating a new commit.


12. Finding and Fixing a Bad Commit (Bisecting)

git bisect start
git bisect bad  # Mark current commit as bad
git bisect good <commit_hash>  # Mark an older commit as good
Enter fullscreen mode Exit fullscreen mode

✅ Helps in debugging when you need to find the exact commit that introduced a bug. Git will guide you through a binary search of commits.


13. Cherry-Picking Specific Commits

git cherry-pick <commit_hash>
Enter fullscreen mode Exit fullscreen mode

✅ Use when you need to apply a specific commit from one branch to another without merging everything.


14. Recovering Deleted Branches

git reflog
git checkout -b recovered_branch <commit_hash>
Enter fullscreen mode Exit fullscreen mode

✅ If you accidentally delete a branch, git reflog helps you find its last commit and recover it.


15. Squashing Multiple Commits Into One

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

✅ Great for cleaning up your commit history before merging a feature branch.


16. Temporarily Shelving Changes (Saving Work Without Committing)

git stash push -m "Work in progress"
git stash list
git stash pop
Enter fullscreen mode Exit fullscreen mode

✅ Ideal when you need to switch branches quickly without committing incomplete work.


17. Undoing a Pushed Commit (Without Affecting Others)

git revert <commit_hash>
git push origin main
Enter fullscreen mode Exit fullscreen mode

✅ Creates a new commit that undoes the changes of a specific commit, keeping history intact.


18. Clearing Local Commits Before Pushing

git reset --soft HEAD~3  # Keeps changes
git reset --hard HEAD~3  # Discards changes
Enter fullscreen mode Exit fullscreen mode

✅ Use --soft to uncommit but keep changes or --hard to erase them completely.


19. Forcing a Pull to Overwrite Local Changes

git fetch --all
git reset --hard origin/main
Enter fullscreen mode Exit fullscreen mode

✅ Use when you want your local branch to exactly match the remote branch, overwriting local changes.


20. Finding Who Made a Change to a Specific Line

git blame -L 42,42 filename.txt
Enter fullscreen mode Exit fullscreen mode

✅ Helps track down who changed a specific line and when, useful for debugging and accountability.


These commands help developers handle tricky Git situations efficiently. Which of these have you used, or do you have any nightmare Git experiences? Let me know! 🚀

Conclusion

Mastering these Git commands will greatly improve your workflow, making you more efficient and productive. Start practicing these commands in your projects and become a Git pro!

📌 What’s your favorite Git command? Let me know in the comments! 🚀

Top comments (0)