These are commands which accomplish basic tasks in the Git version control system, and helped me out of sticky situations when I was learning how to use Git (with Gerrit).
I used to look at this list a lot, but ever since I started using GitHub at work (January 2016), I’ve stopped. I probably figured some of these out myself, but the majority come from the Internet, specifically Stack Overflow.
Commands/tips are in no particular order.
Text preceded by a #
are comments.
Text between angle brackets (< >
) needs to be replaced based on your circumstances.
git branch
Remotes are where you are pushing to and pulling from.
git remote -vv
git -h
# use the --dry-run flag, e.g. before running "git add .":
git add . --dry-run
git clean -h
git push origin HEAD:refs/heads/branch-name
git commit --amend # Will amend the previous commit
OR
git rebase -i
git pull --rebase # commit unpushed changes
git add
git rebase --continue
git push origin HEAD:refs/for/main
git rebase origin/main
git reflog my-branch # shows pointers
git reset --hard HEAD~2
OR
git reflog git reset --hard HEAD@{x}
git checkout -b mergeBranch # save those commits to this branch
Resolve the conflicts. Check out main
and pull. Check out mergeBranch
.
git rebase origin/main # the commits should be on mergeBranch now
git merge --squash mergeBranch
If the commits are beside each other on the branch (check with “git log”), just do:
git rebase -i
git rebase -i HEAD~2 # git rebase -i looks on the remote for unpushed commits
git reset --soft HEAD~2 && git commit
git diff --stat
git branch -d my-local-branch
git branch -m newName
git fetch git rebase -i HEAD~2 # work with the last 2 commits
See the options for squashing (replace “pick” on the bottom commit with “s”). Edit the commit message and push.
Ctrl + R in the terminal
git rerere
git checkout --
git fetch origin
git merge main # Fix conflicts; HEAD = my changes
git add .
git commit
git checkout main
git merge -s ours my-branch
git checkout my-branch
git merge main
git branch -D main # delete your local main branch
git checkout -b main remotes/upstream/main # pull it back down from the remote repo
Use with caution.
git clean -fd
https://stackoverflow.com/questions/41955765/git-remove-all-commits-from-pr
git reset --hard HEAD~3
git push -f origin HEAD
In this example, you have a QA branch named qa
which is missing commits from main or otherwise misaligned, and you want to refresh it with the contents of main. This will not preserve the commit history of qa
git checkout main
git pull origin main
git branch -D qa
git checkout -b qa
git push origin qa -f
git fetch
git checkout qa
git merge origin/bug/JIRA-123-fix-this # origin/topic/branch
git push origin qa
git checkout master
git pull
git checkout pr-branch
git rebase master
git push origin pr-branch
git rebase HEAD~n
<edit the commits and drop them>
git rebase --continue
git push -f origin pr-branch
Don't rebase!
git checkout master
git pull
git checkout pr-branch
git merge master
I used these specifically in the context of a Gerrit workflow, but they can be adapted.
git checkout main # or the branch that the Gerrit commit was on
git checkout -b amendChange # or any other temp name
git fetch # get from Gerrit - Download > cherry pick > clipboard
git cherry-pick pushedCommit # the branch that the change was committed to
git rebase -i HEAD~2
git push origin HEAD:refs/for/main
git fetch
git merge origin/main
git push origin HEAD:refs/heads/ # "/heads/" will skip gerrit for merge commit
git push origin HEAD:refs/for/main # Go through Gerrit
- Add a table of contents