Git cheatsheet

Using Git

Since I’m using git a little bit more often now, this is just a little note to remind me how to do things in git that I would usually do in svn or hg. Yet another git cheatsheet!


This is quite straightforward for my purposes:

git reset --hard HEAD

Running just git reset -h gives:

usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
   or: git reset [-q] <commit> [--] <paths>...
   or: git reset --patch [<commit>] [--] [<paths>...]

    -q, --quiet           be quiet, only report errors
    --mixed               reset HEAD and index
    --soft                reset only HEAD
    --hard                reset HEAD, index and working tree
    --merge               reset HEAD, index and working tree
    --keep                reset HEAD but keep local changes
    -p, --patch           select hunks interactively

Cleaning up

To revert files use:

git clean

with -h:

usage: git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...

    -q, --quiet           do not print names of files removed
    -n, --dry-run         dry run
    -f, --force           force
    -d                    remove whole directories
    -e, --exclude <pattern>
                          add <pattern> to ignore rules
    -x                    remove ignored files, too
    -X                    remove only ignored files

One nice feature is that you can get back to a clean working copy by doing:

git clean -f -x

Moving and Committing.

One very nice feature of git is that you can move files around using the operating system commands, and git tracks the moves automatically.

Untracked files still need to be added via git add, and then git commit --all is the closest analogy to svn commit.

Although a little confusing at first, most git commands do nothing unless you specify a particular argument. Compare this to Subversion: I can’t recall the number of times that people have made accidental commits because Subversion just blindly commits everything.

© 2017