Git has become an indispensable tool for developers worldwide, providing powerful version control capabilities that streamline collaboration and code management. Whether you’re a beginner or a seasoned engineer, mastering Git commands can significantly enhance your productivity and efficiency. In this blog post, we’ll delve into the top 20 Git commands that every senior engineer should have in their toolkit.
1. git init
Before you can start tracking changes, you need to initialize a new Git repository. The git init
command creates a new .git
subdirectory in your project, which stores all the metadata necessary for version control.
git init
2. git clone
To get a copy of an existing repository, use git clone
. This command not only downloads the repository but also sets up a remote tracking branch for you.
git clone <repository-url>
3. git add
Before committing changes, you need to stage them. The git add
command stages files, preparing them for the next commit.
git add <file-name>
# or to add all changes
git add .
4. git commit
Committing changes is like taking a snapshot of your project at a specific point in time. Use git commit
to save your staged changes to the repository.
git commit -m "Your commit message here"
5. git status
To see what changes have been staged, unstaged, and which files aren’t being tracked by Git, use git status
.
git status
6. git log
The git log
command displays a list of commits in the repository, showing the commit hash, author, date, and message. This is invaluable for understanding the project’s history.
git log
7. git branch
Managing branches is a core Git functionality. The git branch
command allows you to create, list, and delete branches.
git branch <branch-name> # Create a new branch
git branch -d <branch-name> # Delete a branch
git branch # List all branches
8. git checkout
Switching between branches or restoring files is done using git checkout
.
git checkout <branch-name> # Switch to a branch
git checkout -- <file-name> # Discard changes to a file
9. git merge
Combining changes from different branches is done with git merge
. This command is essential for integrating features or fixes into the main codebase.
git merge <branch-name>
10. git pull
To fetch and merge changes from a remote repository into your current branch, use git pull
. This is equivalent to running git fetch
followed by git merge
.
git pull <remote-name> <branch-name>
11. git push
After committing your changes locally, you’ll want to push them to a remote repository. The git push
command does just that.
git push <remote-name> <branch-name>
12. git remote
Managing remote repositories is crucial for collaboration. The git remote
command helps you add, view, and delete connections to remote repositories.
git remote add <remote-name> <repository-url>
git remote -v # List remote repositories
13. git fetch
Before merging or pulling changes from a remote repository, it’s often useful to fetch them first. The git fetch
command downloads the latest changes without integrating them into your working directory.
git fetch <remote-name>
14. git diff
To see the differences between your working directory and the staging area, or between different commits, use git diff
.
git diff # Changes not staged for commit
git diff --staged # Changes to be committed
git diff <commit-hash1> <commit-hash2> # Differences between two commits
15. git reset
Sometimes, you need to undo changes. The git reset
command allows you to reset your current branch to a specific state, which can be useful for undoing commits or unstaging files.
git reset <commit-hash> # Reset to a specific commit
git reset HEAD <file-name> # Unstage a file
16. git rebase
The git rebase
command is an alternative to git merge
for integrating changes from one branch into another. It rewrites project history by creating new commits for each commit in the original branch.
git rebase <branch-name>
17. git stash
If you need to switch branches but don’t want to commit your current changes, you can temporarily store them with git stash
.
git stash # Stash changes
git stash pop # Apply stashed changes back to your working directory
18. git tag
Tags are useful for marking important points in your project’s history, such as releases. The git tag
command allows you to create, list, and delete tags.
git tag <tag-name> # Create a lightweight tag
git tag -a <tag-name> -m "Tag message" # Create an annotated tag
git tag # List tags
19. git cherry-pick
Sometimes, you need to apply a specific commit from one branch to another. The git cherry-pick
command does just that, allowing you to pick and apply individual commits.
git cherry-pick <commit-hash>
20. git blame
When you need to find out who made a specific change in a file, git blame
is your go-to command. It shows you the author of each line in a file, along with the commit hash and timestamp.
git blame <file-name>
Conclusion
Mastering these top 20 Git commands will empower you to handle complex version control scenarios with ease. Whether you’re managing branches, merging changes, or troubleshooting issues, these commands form the foundation of effective Git usage. As you continue to work with Git, you’ll discover even more powerful features and techniques to enhance your workflow. Happy coding!
Resources
Would you like a deeper dive into any of these commands? Let me know in the comments! 🚀