Working Directory | Staging area | .git repository |
---|---|---|
<= | = checkout= | =< |
stage >= | => | |
commit >= | => |
Per-project: .git/config
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
$ git config --global core.editor vim
$ git config --list
$ git init
$ git clone https://github.com/libgit2/libgit2 [mygitlib]
Untracked | Unmodified | Modified | Staged |
---|---|---|---|
add the file >= | == | == | => |
edit the file >= | => | ||
stage the file >= | => | ||
<= | =< remove the file | ||
<= | == | =< commit the file |
$ git status
$ git add .
$ git add README.md
$ git add *.c
$ git add modified.file
git status -s
*.a
: no a-files...!lib.a
: ...except for lib.a/TODO
: ignore TODO in the current directory, but not in other directoriesbuild/
: ignore all files in the build directorydoc/*.txt
: ignore txt files in the doc directorydoc/**/*.pdf
: ignore txt files in doc and all its subdirs$ git diff
$ git diff --staged
or $ git diff --cached
$ git commit
or $ got commit -m "my message"
$ git commit -a -m "my message"
git add .
followed by $ git commit -m "my message"
$ git rm
removes files from the staging area and removes it from the drive.$ rm
the change will be recorded. You must still type $ git rm
to remove it from the stage area before committing.$ git rm --cached
$ git mv a b
$ mv a b
followed by $ git rm a && git add b
$ git log
$ git log -p -N
$ git log --stat
use --pretty=keyword option. Keywords are
format
$ git log --pretty=format:"%h - %an, %ar : %s"
%H, %h: Commit hash, abbrev. commit hash
%T, %t: Tree hash, abbrev. tree hash
%P, %p: Parent hash, abbrev. parent hash
%an, %ae: Author name, author email
%ad, %ar: Author date, author date, relative
%cn, %ce: Committer name, committer email
%cd, %cr: Committer date, committer date, relative
%s: Subject
Show graph: git log --pretty=format:"%h %s" --graph
other options:
-(n)
show only the last n commits--since
, --after
Limit to commits after a date--until
, --before
Limit to commits before a date--author
Filter by author--committer
Filter by committer--grep
Filter by content of the commit string-S
Only show commits adding or removing code matching the string$ git commit --amend
to commit remaining files. Works only, if no changes were made since last commit.$ git reset HEAD <file>
'git checkout -- <file>
. Consider branching or stashing instead.$ git remote -v
$ git remote add <handle> <url>
$ git fetch <handle>
$ git pull <handle>
$ git push origin master
$ git remote show origin
$ git remote rename pb paul
$ git remote remove paul
$ git tag
$ git tag -l "v1.8.5*"
-m
specifies the tag message.$ git tag -a v1.4 -m "my version 1.4"
$ git tag v1.4-lw
$ git show v1.4
You can tag after other commits have been made:
$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
$ git tag -a v1.2 a6b4c9
sharing tags with remotes: $ git push origin v1.5
to push all tags at once: $ git push origin --tags
You can't check out tags in git. You must checkout a branch at a specific tag:
$ git checkout -b version2 v2.0.0
$ git config --global alias.co checkout
master
$ git branch testing
$ git checkout testing
$ git checkout -b testing
$ git merge testing
$ git mergetool
$ git branch
gives a list of current branches.$ git branch -v
gives the same list, but with the latest commit messages.$ git branch --merged
$ git branch --un-merged
$ git remote show [remote]