// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

17 Useful git Commands That I Can't Remember

17 useful git commands that are hard to remember, from undoing a commit and stashing unstaged files to deleting remote branches. Dangerous ones are flagged.

Art+Logic Jan 13, 20142 min read

From the Art+Logic archive · written in 2014. The tools may have moved on; the thinking still holds.

Photo of Elephant by Nam Anh on Unsplash

I’m thankful for git for many reasons, like fast commits, offline history browsing, and cheap branching. But one complaint I have is the illogical naming and arguments of git commands.

I’ve long kept a list of useful but hard to remember commands in a notepad.  Here’s my list, roughly sorted by most frequently used.  Careful with the dangerous commands at the bottom.

Common git commands

Tag and share a release

git tag -a release/1.0.0b1 -m "1.0.0b1 release"
git push --tags
git show-ref --tags 1.0.0b1

Undo the last local commit

git reset --soft HEAD^
git reset HEAD .

Stash only unstaged files

git stash save --keep-index

Stage all files, included deletions

git add -u

Revert a commit

If a commit has already been shared publicly, this creates a changeset that reverses the commit.

git revert COMMITHASH

Undo a merge

git merge --abort

View the url of a remote repository

git remote -v

or

git remote show origin

Show the current branch name

git rev-parse --abbrev-ref HEAD

Push a local branch

git push origin BRANCH

List remote branches

git branch -r

(or -a for all branches)

Delete a remote branch

git push origin :BRANCH

Delete local tracking branches

Fixes a common error about deleting a branch from origin when another user has already deleted that remote branch.

git fetch -p origin

Compare commits on two branches

git diff BRANCH1..BRANCH2

Compare commit messages on two branches

git log --cherry-mark --oneline BRANCH1..BRANCH2

Delete a tag

¡dangerous! This rewrites the public repository and breaks other users’ clones if they have already pulled this tag.

git tag -d 12345
git push origin :refs/tags/12345

Move commits to a new branch

¡dangerous! You will lose uncommitted work. Only works for moving commits to a new branch. Moves all commits not on BRANCHNAME to NEWBRANCHNAME. Hat tip to sykora’s SO answer.

git branch NEWBRANCH
git reset --hard origin/BRANCH
git checkout NEWBRANCH

Delete untracked files

¡dangerous! This permanently deletes untracked files.  But the -n flag does a dry-run to help catch mistakes.

git clean -fn

Git alias

Git also provides an alias feature, which could be used to create more memorable commands.  Here’s my simplistic set of aliases in ~/.gitconfig , mostly geared towards reducing typing for common commands:

[alias]
    st = status
    ci = commit
    co = checkout
    df = diff
    lg = log -p
keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…