In today's development landscape, Git has become an indispensable tool for developers at every level, from newcomers to seasoned professionals. It's a tool we rely on daily, but have you ever wondered about the lesser-known Git commands that could enhance your workflow?
Let's explore 12 Git commands that might just surprise you with their usefulness and efficiency.
1. Git stash
Git stash
is useful commands that help you in Git workflows with many branches and many people effectively. In the real world, have ever seen a case that you are developing on the branch and your leader said “hey man, I need your urgent fix on another branch”, how to save your current version code locally but not make commit?
//save unchange commits
git stash
//restore commits
git stash pop
2. Git diff
In case you need to compare between un-saved changes and the previous commit, git diff
is the command you should know in using Git with commands besides using GUI or Git extension.
git diff
3. Git rebase
Rebasing is a powerful besides on Merge feature in Git with the same purpose but with different methods. The most awesome feature that git rebase
brings is keeping the Git tree clean, straight and avoiding “the diamond shape” in the Git graph.
//checkout branch featuire
git switch feature
//rebase feature into main
git rebase -i main feature
4. Git cherry-pick
The git cherry-pick
command is used to apply a specific commit from one branch to another. This can be useful when you want to selectively bring in changes from one branch to another without merging the entire branch.
git cherry-pick <commit-hash>
5. Git log
With git log
, you can show all commit logs in Git including commit hash, commit message, commit author, and the date that the commit was committed.
git log
6. Git commit —amend
If you ask how can I change my commit information, the git commit --amend
is your answer. You can change information of that commit including message and author.
git commit --amend -m "New commit message" --author="Author Name <email@example.com>"
7. Git tag
Tagging versions in Git using the git tag
command is a common practice to mark significant points in your repository, such as releases or milestones. You can tag commits with version numbers or any other meaningful identifier.
git tag -a v1.0 -m "Release version 1.0" <commit-hash>
8. Git checkout .
In case you need to clean up all changes locally or add code to show logs on the server for tracing errors and hot fixing, git checkout .
is the powerful command that you should know.
git checkout .
9. Git reset
git reset
is a powerful command in Git that allows you to reset the current HEAD to a specified state. It's commonly used to undo changes, move the HEAD to a different commit, or unstaged changes.
If you want to move the HEAD to a specific commit but keep the changes in your working directory and staging area, you can use soft reset. If you want to completely undo the last commit and discard the changes from that commit, use git reset hard.
//soft reset
git reset --soft <commit-hash>
//hard reset
git reset --hard HEAD^
10. Git revert
git revert
is a command used to create a new commit that undoes the changes made by a specific commit or commits. Unlike git reset
, which alters the commit history, git revert
maintains the history by creating a new commit that undoes the changes introduced by previous commits.
git revert <commit-hash>
11. Git remote —prune
The git remote --prune
command is used to remove any remote-tracking references that no longer exist on the remote repository. Over time, these references can become outdated if branches are deleted or renamed on the remote repository.
git remote prune origin
With this command, you can sync up locally with your remote git workspace.
12. Git filter-branch
git filter-branch
is a powerful but potentially dangerous command in Git that allows you to rewrite the commit history of a repository. It's typically used to rewrite commits to apply various filters or transformations.
This command can be useful for tasks such as removing sensitive data from the history, rewriting commit messages, or splitting a repository into multiple repositories.
For example, in case you need to rewrite the author of commit history in the repository, you can use this command followed in below script.
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your_old_email"
CORRECT_NAME="your_new_name"
CORRECT_EMAIL="your_new_email"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]; then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]; then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
Top comments (5)
You miss: git reflog
It's powerful but I really missed it. LOL!
So do you will add it? :)
One I use all the time:
git log --oneline --graph --decorate
( use --all as well )
Gives you a very quick way to see the tree on command line with ASCII art if you don't have a GUI IDE to hand.
Yeah, it is so useful!
Do you know more other awesome git commands? And, which is your favorite?