git Frequently Asked Questions

By Xah Lee. Date: . Last updated: .

For git basics, see: git Tutorial.

How to fix last commit message?

git commit --amend -m "new commit message"

How to uncommit last commit?

# reset to last commit, but keep your changes
git reset --soft HEAD^
# reset to last commit, THROW AWAY ALL YOUR CHANGES
git reset --hard HEAD^

How to unadd a file?

# unadd a file
git reset myFileName
# unadd all added files in current dir
git reset .

What does HEAD mean?

HEAD is a named reference to a specific commit. It USUALLY reference to the last commit of your local repository.

It is a file at .git/HEAD. Normally, that file's content is:

ref: refs/heads/master

and if you look at the file it points to .git/refs/heads/master, the file content is a commit ID, for example: 62e30e69a0a6f9ea2834ae99402c85e66a2a22bc

Normally, when calling a git command that needs a commit ID, you can use HEAD as the last commit ID.

You can use git-reset to set HEAD. See man git-reset, man gitglossary.

What is git {origin, master, object, index}?

See: man 7 gitglossary

What is the difference between clone and checkout

git clone is for getting a new repository from a remote git server.

git checkout is to checkout from a older commit or different branch to your local repository. Use option “-f” (force) to discard any changes you made in your working directory.

You can call git log first to get a list of commit ID. Then use git checkout -f commit_ID to update your local repository (if you want to look at old version of files). Then, git checkout -f master to revert back to your current state.

# example of git checkout old commit
git checkout -f 9890e063897b2f7f40c31af513b3f1ada243915f
# example of making local git branch the master branch
git checkout -f master

How to make git always use color in diff?

git config --global color.ui true

How to set git output non-interactive?

git log | cat


git --no-pager log

How to make git display date in ISO format?

Use --date=iso.

git log --date=iso

How to find out when is a line added to a file?

git blame file_name → Show what revision and author last modified each line of a file.


git log -S search_string file_name

git log -G regex file_name

See man git-blame and man git-log.

Move and Rename File

You shouldn't need to rename or move files. Just use normal unix/OS commands {rm, mv} to {delete, rename, move} files in your working directory. Then, when you are ready to git, do git add -A. The “-A” option will {add, remove} all {new, deleted} files based on your working dir, then you can git commit -m"…".

but if you really want to rename or move, you can do: git mv file_old_name file_new_name

Thanks to Nick Alcock for many tips.

#git is there any tech consequence of not setting name and email? i.e. does it prevent push or something?

Name and email is an enforced convention. You’re supposed to attribute your commits, otherwise collaborators won’t make sense of who did what. The official Git binary does not let you commit unless you have set at least OR it can make a reasonable guess at your email (e.g. if you have a fully-quailified host name set, it will take your <username>@<hostname.domain>).

Git itself does not actually use the emails for any purpose, so, technically, this convention can be circumvented, by creating commit objects programmatically. E.g. git-svn maps Subversion user names to <username>@<guid of svn repository> unless otherwise instructed.

—Yuri Khan , from @