Chris Colborne

Git and Mercurial Cheat Sheet

February 01, 2015

Illustration from unDraw

Our team supports a large number of applications, ranging from legacy apps in SVN, to more recent apps in Mercurial, to our latest projects in Git.

Inspired by the excellent (but very thorough) Mercurial to Git Rosetta stone, here is a bare bones, back to basics, KISS mapping between Hg and Git of the most common operations I use on a day to day basis.

Init

hg initgit init

Add Remote

edit paths in .hg/hgrc git remote add origin [email protected]:<username>/<repo-name>.git

Clone

hg clone <url>git clone <url>

Pull

hg pull -ugit pull

Push

hg pushgit push

Diff

hg diffgit diff
OR
git diff HEAD (to diff both staged and unstaged)
hg diff -r 20:21 ./src/filenamegit diff <hash>..<hash> -- ./src/filename
... coming ...git diff master...<hash> (to diff RHS with common ancestor)

See incoming/outgoing commits

hg ingit fetch && git log master..FETCH_HEAD
hg outgit fetch && git log FETCH_HEAD..master

Commit

hg ci -m"Commit message"git commit -am"Commit message"

Interactively commit part of working copy

hg recordgit add --patch && git commit

Branches

hg branch <branch-name>git checkout -b <branch-name>
hg up <branch-name>git checkout <branch-name>
hg push --new-branchgit push origin <branch-name> -u
hg branchesgit branch -a
hg commit --close-branch# delete local branch
git branch -d <branch-name>
then
# delete remote branch (NB the colon)
git push origin :<branch-name>
also
# remove remote tracking branches that no longer exist
git fetch -p
# show commits from branch
hg log -b <branch-name>
git reflog show <branch-name>

Merge

hg merge <branch-name or commit number/hash>git merge <branch-name or commit hash>

Shelve/stash

hg shelvegit stash
hg unshelvegit stash pop

Clear working copy

hg up -Cgit checkout -f

Rollback

hg rollbackgit reset HEAD~

Remove ignored files that are already committed

hg forget "set:hgignore() and not ignored()"git ls-files --ignored --exclude-standard | xargs git rm --cached

Like what you've read?

Why not subscribe to my mailing list to get my latest articles by email.

I respect the privacy of your email address. You can unsubscribe at any time.

Written by Chris Colborne, an Aussie software engineer from Brisbane, Australia. Follow me on Twitter

Chris Colborne © 2023