Some helpful resources:
Git has 3 places to store files: working dir, stage and history.
The last commit in history is called "HEAD",
all the older commits have their IDs.
In the following texts, "a47c3" and "b325c" are both sample revision IDs,
which can be get with git log --oneline
.
Move Files
-
Working dir -> Stage:
git add -A
for all files,git add -u
for update,git add filename
; -
Stage -> HEAD:
git commit filename
,git commit
for all; -
HEAD -> Stage:
git reset -- files
,git reset
orgit reset -- .
for all; -
Stage -> Working dir:
git checkout -- files
,git checkout -- .
for all; -
HEAD -> ( Stage & Working dir ):
git checkout HEAD -- files
,git checkout HEAD
do nothing; -
Working dir -> ( Stage & HEAD ):
git commit filename
,git commit -a
for all;
Compare Files
-
Working dir <-> Stage:
git diff filename
,git diff
for all; -
Stage <-> HEAD:
git diff --cached filename
,git diff --cached
for all;; -
HEAD <-> Working dir:
git diff HEAD filename
,git diff HEAD
for all; -
Revision a47c3 <-> Working dir:
git diff a47c3 filename
,git diff a47c3
for all; -
Revision a47c3 <-> b325c:
git diff a47c3 b325c filename
,git diff a47c3 b325c
for all; -
Revision X and its previous revision:
git show a47c3
, if X is HEAD:git show
;
If comparing a specific file, always add '-- git show -- filename
, git diff --cached -- filename
.
Show file content
- Show file content of a specific commit:
git show a47c3:filename
;
Logs and Tags
-
Print logs with tags and color highlights:
git log --decorate
; -
Print logs with files changed:
git log --name-status
; -
Print logs in one line for each:
git log --oneline
; -
Print logs with all information:
git log --graph --decorate --name-status --all
; -
Create a tag based on a commit:
git tag 1.0 a47c3
; -
Create a tag on HEAD:
git tag 'Dell-E7450'
; -
List all tags:
git tag
; -
Show tag contents:
git show Dell-E7450
;
Useful Commands
git log -p --all -S 'search string'
: search a string in git history,
see note "Search String Pattern in Git History" for details;
git add <files>
: add files to stage area, use git rm --cached <file>
to remove it from stage area (physical file remains);
git add -A
: If you use "mv" instead of "git mv" to rename a file,
you can use this command to apply this rename operation to stage area;
git checkout <commit>
: make your working directory back to state
$ git checkout <branch-name> // switch between branches
git checkout -- <file-name>
: use
git checkout -- .
: abandon all modifications in working directory.
Don't forget the last dot. It means "all".
git clean -n
: "dry run" mode, preview the removed files;
git clean -f
: remove all untracked files;
git clean -f -d
: remove all untracked files and directories;
git commit
: commit all files in stage area;
git config
: see section "git config" below. list all configs: git config --list
;
git diff
: compare file contents;
git init
: initialize local repository;
git log --stat
: list log with file changes;
git mv <old-file-name> <new-file-name>
git remote show origin
: git equivalent of "svn info";
git reset --hard <commit>
: go back to
git reset
: undo all "git add" commands, same with "git reset HEAD";
git reset -- <file-name>
: only remove
git rm
:
git show <object>
: show information of
git show 389be --stat
or print file contents of a specified version, the following command show the file contents of the parent of HEAD:
git show HEAD~1:donno/cloudsync.py
Note that the colon after version is necessary,
or it will print the difference between HEAD~1 & HEAD~2 when it's
git show HEAD~1 filename
;
git stash
: save current working directory and index
and change them to the state of last commit;
git stash save "some messages"
: stash with some messages;
git stash apply
: restore last "stash save";
git stash drop
: delete last "stash save
git stash pop
: git stash apply + git stash drop
git stash clear
: clear all stash saves;
git status
Undo the last commit
After a commit with git commit -m "..."
,
I found I forget to update version number which is defined in file build.gradle.
Now I need undo the last commit, modify version number in build.gradle,
then commit again with the same commit comments.
$ git reset --soft HEAD~1
// edit build.gradle
$ git add -u
$ git commit -c ORIG_HEAD
Ref: Undo the last Git commit?
GUI tool: gitk
, see all branches with gitk --all
.
Git config
Add the following lines into user level config file ~/.gitconfig, or system level config file /etc/gitconfig:
[user]
name = leetschau
email = leetschau@gmail.com
[alias]
ci = commit -s
co = checkout
br = branch
lg = log --stat --graph --decorate --all
Here in git log
, --stat prints file changed info,
--all print all branches instead of current branch,
--decorate print branch/tag info,
--graph print hierarchical relation lines between commits.
Or change configs in command line:
git config --global alias.ci "commit -s"
git config --global user.name "leetschau"
git config --global user.email "leetschau@gmail.com"