Git is a powerful version control system used by developers worldwide to manage and track changes in their codebases. While most developers are familiar with basic Git commands like git commit
, git push
, and git pull
, there are many advanced commands and techniques that can greatly enhance your workflow and productivity. In this post, we’ll explore 10 lesser-known Git commands and techniques that you probably haven't used yet but can significantly streamline your development process and improve your version control skills. Dive in and discover how these advanced features can help you tackle complex tasks with ease.
git bisect: This command is used for binary search to find the commit that introduced a bug. Start with
git bisect start
, mark the current commit as bad withgit bisect bad
, and mark a known good commit withgit bisect good [commit]
. Git will then help you find the exact commit that introduced the issue.git reflog: This command allows you to view the history of all actions in the local repository, including those that are not part of the commit history, such as resets and rebases. Use
git reflog
to see this history.git rerere: This stands for "reuse recorded resolution." It helps to remember how you resolved a conflict and reuses that resolution if the same conflict happens again. Enable it with
git config --global rerere.enabled true
.git blame -C -C: This variant of
git blame
tracks moved or copied lines of code across files. The-C -C
options increase the likelihood of detecting such changes.git commit --fixup [commit]: This command marks a commit as a fixup of an existing commit. Later, you can use
git rebase -i --autosquash
to automatically squash the fixup commit into the target commit.git worktree: This command allows you to check out multiple branches at once in separate working directories. Use
git worktree add [path] [branch]
to create a new working tree.git stash push -m [message]: This command allows you to stash changes with a message. Use
git stash list
to see the list of stashes with their messages.git cherry-pick -n [commit]: The
-n
(no-commit) option applies the changes from the commit but does not create a commit. This allows you to make additional changes before committing.git filter-branch: This powerful command allows you to rewrite history, such as removing sensitive data or changing author information. Use with caution, as it can rewrite large parts of history.
git clean -fdx: This command removes all untracked files and directories, including ignored ones. Use it to clean your working directory completely.
Top comments (2)
Several of these I knew, and use somewhat regularly, but
git commit --fixup [commit]
is going to a be huge help! I hate it when I notice a typo or something that needs to be in a commit several commits back, and I really hate to create a stupid "Fix typo in x" commit. So I usually go through the pain of reverting back to that commit, making the fix, then re-doing every commit that came after.Yes, very helpful to make sure those messages are correct. Thanks for the read, Scott!