Git Advanced Commands

May 26, 2021 ยท View on GitHub

Branching & Rebasing & squashing

CommandDescription
git checkout [branch name] --orphanCreates a new branch without the long repository's history.
git rebase [source branch] [target branch]Fetch the current commits timeline from the remote branch and apply the new commits from the local branch on top of it
git rebase -i [commit hash]^Interactively rebase starting from the parent commit, where [commit hash]^ specifies the parent commit
git pull origin [source branch] --rebasePull changes from a remote repository using the rebase strategy. Default is merge.
git pull origin [source branch] --rebase --autostashThe same as above but performing automatically stash and pop your uncommitted changes when the branch is in a dirty state.
git cherry-pick [commit hash]Apply the changes from any branche's commit to the target branch.
git config branch.[branch name].rebase trueSet a specific branch to always use the rebase strategy.

Fixing mistakes & destructive

CommandDescription
git reset --hard origin/[target branch]Reset the local branch to the origin branch's state. Caution: your local changes will be lost.
git reset --soft HEAD~1Undo the last local commit. The --soft flag keeps the changes, if you don't need to keep them, you can use the --hard instead. Caution, the latter is a descructive command.
git commit --amendModify the most recent commit. Can be used to edit message or add more files to that commit. Caution, this changes the commit hash, so do this only while it was not yet pushed to a shared branch.
git revert HEAD~1Create a new commit undoing the last commit's changes.
git reflogList all the git actions that were executed before. Useful to find a lost commit, then you can try to recover it.
git bisect startUseful to find the buggy commit (that possible introduces some bug). After this, you use `git bisect [good
git branch --merged | egrep -v "(*|master|dev)" | xargs git branch -dDelete already merged branches but master and dev in this example.
git clean -dfRemove untracked files and directories from the working tree. Add n to the flags to list all files that would be removed.