1. git rebase
Git rebase is a powerful command that allows you to integrate changes from one branch into another, but in a cleaner way than git merge
. Instead of creating a merge commit, it re-applies commits on top of the target branch.
git rebase <branch>
This is especially useful when keeping a linear history in your project is important. Rebasing essentially moves or rebases commits from one branch onto another, avoiding unnecessary merge commits.
2. git reflog
When you need to find a commit that seems "lost" or have overwritten something by mistake, git reflog
can save the day. This command allows you to view all your local repository's history, even if some commits are no longer referenced in your branch history.
git reflog
It tracks every action performed in the Git repository, and you can restore changes even if they've been deleted.
3. git cherry-pick
If you only want to apply specific commits from one branch to another, git cherry-pick
lets you do just that. Instead of merging a full branch, you can copy one or more specific commits.
git cherry-pick <commit-hash>
This command is useful when you need a bug fix or feature from another branch but don't want all the changes that come with a full merge or rebase.
4. git bisect
Finding the exact commit that introduced a bug can be tedious. git bisect
is a binary search tool that helps automate this process. It repeatedly checks out commits in the middle of the history to pinpoint when a bug was introduced.
git bisect start
git bisect bad
git bisect good <commit>
This will narrow down the commit history until you find the problematic commit.
5. git stash
When you’re working on changes but need to switch to another task or branch, you can use git stash
to save your work-in-progress without committing it. It stores changes in a temporary space that you can apply later.
git stash
git stash pop
This is useful when you’re working on something experimental but need to change context temporarily.
6. git reset
git reset
is an advanced tool that allows you to move the current branch to a previous state. The power of this command lies in its three modes: --soft
, --mixed
, and --hard
.
git reset --soft HEAD~1
--soft
mode only changes your commit history but keeps all changes staged. --mixed
unstages the changes, while --hard
removes both commit history and the changes in your working directory.
7. git filter-branch
Sometimes you may need to rewrite history to remove sensitive data or correct mistakes across many commits. git filter-branch
is the go-to command for this. It allows you to apply filters and rewrite the entire commit history of a branch.
git filter-branch --tree-filter 'rm -f <file>' HEAD
Be cautious when using this command, as it rewrites history and can affect your collaboration with others if not handled properly.
8. git blame
To figure out who last modified a line or section of a file, git blame
is your command. It helps identify the author of each line in a file and the commit where the change was introduced.
git blame <file>
This is particularly useful for tracking down the context behind specific code changes.
9. git worktree
If you want to work on multiple branches simultaneously without creating separate clones of your repository, git worktree
is the solution. It allows you to have multiple working directories, each on different branches, all linked to the same Git repository.
git worktree add <directory> <branch>
This way, you can work on different branches in isolation without disturbing your main working directory.
10. git submodule
When your project depends on other Git repositories, git submodule
allows you to include them as subprojects. This is useful for large projects where you need to manage separate repositories as dependencies.
git submodule add <repository-url>
It helps keep the codebases clean and modular by separating dependencies from the main repository.
Conclusion
These advanced Git commands offer tremendous flexibility and control over your project’s version history and workflow. By mastering them, you’ll be able to handle complex version control scenarios with ease. Understanding and using these tools effectively will make you a more efficient and powerful developer.