Skip to content

Handy Git commands. Paste 'n' go. Beginner-friendly explanations. Feel free to contribute.

Notifications You must be signed in to change notification settings

vlvagerviwager/handy-git-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 

Repository files navigation

Handy-Dandy Git Commands

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.


View branches

git branch

View remotes

Remotes are where you are pushing to and pulling from.

git remote -vv

Get information/help on a git command

git -h

Simulate what would happen if a command is run

# use the --dry-run flag, e.g. before running "git add .":
git add . --dry-run

Clean up unindexed files

git clean -h

“Your branch is ahead of origin (remote branch) by x commits”

git push origin HEAD:refs/heads/branch-name

Amend a commit message

git commit --amend # Will amend the previous commit

OR

git rebase -i

Pulling conflicting changes

git pull --rebase # commit unpushed changes
git add
git rebase --continue
git push origin HEAD:refs/for/main

Updating a feature branch with main

git rebase origin/main

Resetting to a particular state

git reflog my-branch # shows pointers
git reset --hard HEAD~2

OR

git reflog git reset --hard HEAD@{x}

Squashing commits

You are on a branch which has a bunch of commits

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

Squashing most recent 2 commits on a local branch

git rebase -i HEAD~2 # git rebase -i looks on the remote for unpushed commits

Squash last 2 commits and write new commit message

git reset --soft HEAD~2 && git commit

Diff

git diff --stat

Delete a local branch

git branch -d my-local-branch

Rename current branch

git branch -m newName

Something messed up - commits stuck/can’t amend/want to squash

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.

Search past commands in Bash

Ctrl + R in the terminal

When rebasing on multiple commits, remember solved merge conflicts

git rerere

Reset one file (uncommitted)

git checkout --

Fix merge conflict

git fetch origin
git merge main # Fix conflicts; HEAD = my changes
git add .
git commit

Replace my-branch completely with main (or any other branch)

git checkout main
git merge -s ours my-branch
git checkout my-branch
git merge main

Reset your local main branch (e.g. if it's irreparably messed up)

git branch -D main # delete your local main branch
git checkout -b main remotes/upstream/main # pull it back down from the remote repo

Revert all uncommitted changes including files and folders (e.g. from git merge)

Use with caution.

git clean -fd

Remove lots of accidental commits pulled into PR

https://stackoverflow.com/questions/41955765/git-remove-all-commits-from-pr

Remove commits that have been pushed

git reset --hard HEAD~3
git push -f origin HEAD 

Replace the contents of one branch with another, erasing commit history

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 

Push a feature branch

git fetch
git checkout qa
git merge origin/bug/JIRA-123-fix-this # origin/topic/branch
git push origin qa

Can’t rebase on GitHub / GitLab

git checkout master
git pull
git checkout pr-branch
git rebase master
git push origin pr-branch

Rogue commits in branch

git rebase HEAD~n
<edit the commits and drop them>
git rebase --continue
git push -f origin pr-branch

The source branch is n commits behind the target branch

Don't rebase!

git checkout master
git pull
git checkout pr-branch
git merge master

Working with forks

Checkout a branch on someone else’s fork

https://stackoverflow.com/questions/9153598/how-do-i-fetch-a-branch-on-someone-elses-fork-on-github/9153737


Working with Gerrit

I used these specifically in the context of a Gerrit workflow, but they can be adapted.

Amend a commit on Gerrit

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

Merge commit feature branch

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

TODO

  • Add a table of contents

About

Handy Git commands. Paste 'n' go. Beginner-friendly explanations. Feel free to contribute.

Topics

Resources

Stars

Watchers

Forks