Git is a popular version control system used by developers worldwide. While most developers are familiar with commonly used Git commands such as git add
, git commit
, and git push
, several lesser-known Git commands can be extremely useful for DevOps engineers. In this article, we'll discover 10 lesser-known Git commands that you can use to improve your workflow.
1. git cherry-pick
The git cherry-pick
command is used to apply a specific commit from one branch to another branch. This is useful when you want to apply a bug fix or feature to a release branch while keeping the changes isolated from other branches. Here's the syntax:
git cherry-pick <commit-hash>
2. git bisect
The git bisect
command is used to find the commit that introduced a bug in your codebase. This command uses a binary search algorithm to help you quickly narrow down the problematic commit by checking out revisions and asking you to evaluate them. Here's the syntax:
git bisect start
git bisect bad <bad-commit-hash>
git bisect good <good-commit-hash>
3. git rebase
The git rebase
command is used to integrate changes from one branch into another. This command replays all the commits made in one branch onto another branch, ensuring that the resulting commit graph is linear. Here's the syntax:
git rebase <branch-to-rebase-onto>
4. git stash
The git stash
command is used to save changes that aren't ready to be committed yet. This command will save the changes to a "stash" and remove them from your working directory. You can then apply the changes later using git stash apply
. Here's the syntax:
git stash save "some message"
git stash apply
5. git reflog
The git reflog
command is used to review the history of your Git commands. This command shows a log of all the references that Git knows about, including commits, merges, and other types of references. This command can help you recover lost commits or branches. Here's the syntax:
git reflog
6. git log
The git log
command is used to review the history of commits in your Git repository. This command shows a list of all the commits in reverse chronological order, including their commit hash, author, and timestamp. This command can help you understand the changes made to your codebase over time. Here's the syntax:
git log
7. git archive
The git archive
command is used to export the contents of a Git repository as a tar or zip archive. This command can be useful when you want to distribute a specific version of your codebase to someone who doesn't use Git. Here's the syntax:
git archive --format=tar HEAD | gzip > archive.tar.gz
8. git blame
The git blame
command is used to determine who last modified each line of a file in your Git repository. This command can help you understand why a specific line of code was added or modified. Here's the syntax:
git blame <filename>
9. git clean
The git clean
command is used to remove untracked files and directories from your working directory. This command is useful when you want to ensure that your working directory is clean before switching to a different branch. Here's the syntax:
git clean -f
10. git notes
The git notes
command is used to add extra annotations to a commit without changing the commit hash. This command can be used to add information, such as who reviewed the commit or which issue it addresses. Here's the syntax:
git notes add <commit-hash> -m "some note"
Conclusion
In this article, we explored 10 uncommon Git commands that can help improve your workflow as a DevOps engineer. While these commands might not be used frequently, they can be extremely useful in certain situations. By adding them to your knowledge base, you can become a more effective developer and troubleshooter.