Top 20 Git Commands You Must Know (With Examples)
Top 20 Git Commands with Examples
Git is one of the most widely used version control systems, allowing developers to track changes, collaborate, and manage their code efficiently.
Whether you are a beginner or an experienced developer, knowing essential Git commands is crucial for efficient workflow. In this blog post, we’ll explore the top 20 Git commands with examples to help you master Git.
1. git init
Initializes a new Git repository in the current directory.
$ git init
2. git clone
Creates a copy of an existing repository.
$ git clone https://github.com/user/repository.git
3. git status
Shows the status of the working directory and staging area.
$ git status
4. git add
Stages changes for the next commit.
$ git add filename
$ git add . # Stages all changes
5. git commit
Saves staged changes to the repository.
$ git commit -m "Your commit message"
6. git log
Displays the commit history.
$ git log
7. git branch
Lists, creates, or deletes branches.
$ git branch # List branches
$ git branch new-branch # Create a new branch
$ git branch -d branch-name # Delete a branch
8. git checkout
Switches between branches.
$ git checkout branch-name
9. git merge
Merges a branch into the current branch.
$ git merge branch-name
10. git pull
Fetches and integrates changes from a remote repository.
$ git pull origin main
11. git push
Uploads local changes to a remote repository.
$ git push origin branch-name
12. git remote
Manages connections to remote repositories.
$ git remote -v # List remotes
$ git remote add origin https://github.com/user/repository.git # Add a remote
13. git reset
Resets changes in the working directory or staging area.
$ git reset filename # Unstage a file
$ git reset --hard # Reset everything
14. git revert
Creates a new commit that undoes changes from a previous commit.
$ git revert commit-hash
15. git stash
Temporarily saves changes without committing them.
$ git stash
$ git stash pop # Apply stashed changes
16. git diff
Shows differences between files.
$ git diff # View unstaged changes
17. git fetch
Retrieves changes from a remote repository but does not merge them.
$ git fetch origin
18. git tag
Creates tags to mark specific commits.
$ git tag v1.0 # Create a tag
$ git tag # List all tags
19. git cherry-pick
Applies changes from a specific commit to the current branch.
$ git cherry-pick commit-hash
20. git rebase
Reapplies commits on top of another base commit.
$ git rebase branch-name
Conclusion
Git is a powerful tool for version control, and mastering these commands will help you efficiently manage your projects.
Whether you’re working alone or collaborating with a team, understanding these essential Git commands ensures a smoother development process.
Which Git command do you use the most? Let us know in the comments! 🚀