git Frequently Asked Questions

By Xah Lee. Date: . Last updated: .

work with git repo without cd into the dir

use option --git-dir=path/.git/ Example:

git --git-dir=/Volumes/backup/web/.git/ pull /Users/joe/web/

fix last commit message

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

undo 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^

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.

[see Git: What's HEAD]

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

origin
The default upstream repository. Most projects have at least one upstream project which they track. By default origin is used for that purpose. New upstream updates will be fetched into remote remote-tracking branches named origin/name-of-upstream-branch, which you can see using git branch -r.
master
Whenever you create a git repository, a branch named “master” is created, and becomes the active branch. [see Git Branching]
index
(aka staging area) A collection of files with stat information, whose contents are stored as objects. The index is a stored version of your working tree. Truth be told, it can also contain a second, and even a third version of a working tree, which are used when merging.
object
The unit of storage in git. It is uniquely identified by the SHA1 of its contents. Consequently, an object can not be changed.
object database
Stores a set of "objects", and an individual object is identified by its object name. The objects usually live in $GIT_DIR/objects/.
object name
The unique identifier of an object. The hash of the object’s contents using the Secure Hash Algorithm 1 and usually represented by the 40 character hexadecimal encoding of the hash of the object.
pack
A set of objects which have been compressed into one file (to save space or to transmit them efficiently).

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

make git always use color in diff

git config --global color.ui true

set git output to non-interactive

git --no-pager log

or set in config:

git config --global core.pager ''

make git display date in ISO format

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.

or

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 https://plus.google.com/115849739354666812574/posts] for many tips.

git newline config for Microsoft Windows

git config --global core.autocrlf true
Convert LF to CRLF when check out.
git config --global core.autocrlf input
Convert CRLF to LF when check in.
git config --global core.autocrlf false
Do nothing.

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 user.email OR it can make a reasonable guess at your email (example: 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 @ https://plus.google.com/u/0/+XahLee/posts/2swUrf372Ar

git