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 branchRemotes are where you are pushing to and pulling from.
git remote -vvgit -h# use the --dry-run flag, e.g. before running "git add .":
git add . --dry-rungit clean -hgit push origin HEAD:refs/heads/branch-namegit commit --amend # Will amend the previous commitOR
git rebase -igit pull --rebase # commit unpushed changes
git add
git rebase --continue
git push origin HEAD:refs/for/maingit rebase origin/maingit reflog my-branch # shows pointers
git reset --hard HEAD~2OR
git reflog git reset --hard HEAD@{x}git checkout -b mergeBranch # save those commits to this branchResolve the conflicts. Check out main and pull. Check out mergeBranch.
git rebase origin/main # the commits should be on mergeBranch now
git merge --squash mergeBranchIf the commits are beside each other on the branch (check with “git log”), just do:
git rebase -igit rebase -i HEAD~2 # git rebase -i looks on the remote for unpushed commitsgit reset --soft HEAD~2 && git commitgit diff --statgit branch -d my-local-branchgit branch -m newNamegit fetch git rebase -i HEAD~2 # work with the last 2 commitsSee the options for squashing (replace “pick” on the bottom commit with “s”). Edit the commit message and push.
Ctrl + R in the terminal
git rereregit checkout --git fetch origin
git merge main # Fix conflicts; HEAD = my changes
git add .
git commitgit checkout main
git merge -s ours my-branch
git checkout my-branch
git merge maingit branch -D main # delete your local main branch
git checkout -b main remotes/upstream/main # pull it back down from the remote repoUse with caution.
git clean -fdhttps://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 qagit checkout master
git pull
git checkout pr-branch
git rebase master
git push origin pr-branchgit rebase HEAD~n
<edit the commits and drop them>
git rebase --continue
git push -f origin pr-branchDon't rebase!
git checkout master
git pull
git checkout pr-branch
git merge masterI 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/maingit 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