Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > Programming > git Usage

git Usage

3 places

  • working directory: the place you work on the files
  • stage (index): directory storing files temporarily
  • history: repository

configure git after installation

git config --global --edit
# then edit the file by entering your name and email in the template

create version controlled dir

mkdir test
cd test
git init

add file

git add file1.txt
git add file2.txt file3.txt
git add *   # add all new/modified files & directories
git commit -m 'my comment'

  • git add: submit changes in working dir to stage
  • git commit: submit changes in stage to history

check status

git status

check modification

git diff file1.txt   # diff between stage and wroking dir
git diff <branch>   # diff between <branch> and working dir
git diff HEAD      # diff between HEAD and working dir
git diff <commit1> <commit2>
git diff --cached   # diff between HEAD and stage

check in modified files

git add file1.txt
git commit -m 'my modification'

'git add' puts changes to 'stage'; 'git commit' submits the stage, and 'HEAD' points to the latest result.

check file/repository history

git log file1.txt
git log
git log --pretty=oneline # simple output

note that version number in git is not sequential number, but a number computed with SHA1 algorithm.

check command history

git reflog

This also shows the version number (aka 'commit'), which can be used to go to a certain version.

go back to a prev version

git reset --hard version

Here, 'version' can be 'HEAD^' (prev version), 'HEAD^^' (version before the prev version), ..., 'HEAD~100' (to avoid too many '^' characters).

If we'd like to go back again to the last version, we can use 'git reflog' to retrieve the last commit (version number), and do 'git reset --hard last_commit' again.

discard modifications in working directory

git checkout -- file1.txt

Note: Double dash ("--") is basically a separator between options and parameters. In some situations it is not needed (optional), but it can be used to make sure git won't interpret some special file names as options.

Note: difference between reset and checkout:

  • git reset: undo the most recent 'git add' by replacing files in stage with the ones in history
  • git checkout: undo the most recent changes in working dir by replacing files with those in stage.
  • git checkout HEAD: do both of the above two tasks.

git add -- -wierd_name.txt

revert modifications added to stage

git reset HEAD file1.txt   # undo 'git add file1.txt'
git checkout -- file1.txt   # undo the modification on file1.txt

revert committed modifications

git reset --hard HEAD^    # supposing it's not been sent to remote repository yet

commit directly

From working dir to history directory.

git commit file1.txt file2.txt
git commit -a

replace working dir with latest committed version

git checkout HEAD -- file1.txt file2.txt

delete file from repository

git rm file2.txt
git commit -m 'delete file'

git rm file1.txt
# undo the above operation
git reset HEAD file1.txt
git checkout -- file1.txt

git rm file1.txt
git commit -m 'delete it'
# undo the above operations
git reset --hard HEAD^


Useful Links