GIT Commands
Convention in GIT
master = branch master
HEAD = alias/pointer to latest commit
HEAD^ or HEAD~1 = alias/pointer to second latest commit
origin = remote path
Create remote repo
On remote machine (Case use bare as central repo)
# ssh git@example.com
# mkdir my_project.git
# cd my_project.git
# git init --bare --shared=group # Directory hooks/, info/, objects/, refs/, HEAD, description, config will be created
# git repo-config core.sharedRepository "group"
# chgrp -R gitgrp .
# chmod -R g+ws .
# git-update-server-info # If plan to serve via HTTP
# exit
On local machine
$ cd my_project
$ git init
$ git add *
$ git commit -m "Initial import"
$ git remote add origin git@example.com:my_project.git
$ git remote add origin file:///home/repo/git/my_project.git
$ git remote add origin ssh://user@xx.xx.xx.xx/home/repo/git/my_project.git
$ git push origin master
Checkout
$ mkdir ~/git_sandbox
$ cd ~/git_sandbox
$ git clone file:///home/repo/git/my_project.git
$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master
On remote machine (Case use non-bare as central repo)
# mkdir my_project
# cd my_project
# git init # Directory .git will be created
# git config receive.denyCurrentBranch "ignore" # in order to let others do git push
# git repo-config core.sharedRepository "group"
# chgrp -R gitgrp .
# chmod -R g+ws .
To let others do git push in bare repo
cd <repo.git>/ # Enter inside the git repo
git config core.sharedRepository group # Update the git's config
chgrp -R <group-name> . # Change files and directories' group
chmod -R g+w . # Change permissions
chmod g-w objects/pack/* # Git pack files should be immutable
chmod g+s `find . -type d` # New files get group id of directory
where:
<repo.git> is the bare repository directory, typically on the server (e.g. my_project.git/).
<group-name> is the group name for git users (e.g. users).
To let others do git push in non-bare repo
cd <project_dir>/ # Enter inside the project directory
git config core.sharedRepository group # Update the git's config
chgrp -R <group-name> . # Change files and directories' group
chmod -R g+w . # Change permissions
chmod g-w .git/objects/pack/* # Git pack files should be immutable
chmod g+s `find . -type d` # New files get group id of directory
where:
<project_dir> is the project directory containing the .git folder.
<group-name> is the group name for git users (e.g. users).
Configure git difftool
$ git config --global diff.tool vimdiff
$ git config --global merge.tool vimdiff
$ git config --global --add difftool.prompt false
List current work directory condition
$ git status
List current work directory condition, with only filename
$ git status -s | cut -c4-
$ git status --porcelain | sed s/^...//
$ git diff --name-only
List remote aliases
$ git remote -v
List remote branches
$ git remote show origin
Different (work tree vs staged)
$ git diff
Different (work tree vs remote HEAD)
$ git diff HEAD
Different (staged vs remote HEAD)
$ git diff --cached
Show history of last commit
$ git show HEAD
Show log
$ git log
Show log with files changes
$ git log --name-status
Show log with author, with graph, one line description, 5 lines
$ git log --author=Linus --graph --oneline -5
Add file to staged/cached
$ git add <filename>
$ git add -i # add interactively
Add files/folder except few
$ git add . # add to index only files created/modified and not those deleted
$ git add -u # add to index only files deleted/modified and not those created (Files that already under git control)
$ git add -A # do both operation at once, add to index all files
$ git add --all # do both operation at once, add to index all files
Add files that are already under git control
$ git ls-files --modified | xargs git add
$ git ls-files -m | xargs git add
Undo add file to staged/cached
$ git reset <filename>
$ git reset HEAD <filename>
Delete file
$ git rm <filename>
Commit
$ git commit -a -m "added key scanning"
$ git push origin master
Update ("fetch" mean get latest change with no merge, "pull' mean get latest and merge)
$ git fetch
$ git pull (actually it do git fetch, follow by git merge)
Revert file
$ git checkout <filename>
$ git checkout HEAD <filename>
Removes staged and working directory changes
$ git reset --hard
Removes local commit (before git push)
$ git reset --hard HEAD^
$ git reset --hard HEAD~1
Removes commit (after git push)
$ git revert hash
Blame a file
$ git blame ui_key.c
Tag a revision
$ git tag <RELEASE_V05>
List tag revision
$ git tag -l
Checkout specific tag revision
$ git checkout <tagname>
Create branch
$ git branch <new-br-name>
Switch to branch
$ git checkout <br-name>
Create branch and switch to it
$ git checkout -b <new-br-name> <base>
(actually it do git checkout <base>, follow by git branch <new-br-name>, follow by git checkout <new-br-name>)
Delete branch
$ git branch -d <br-name>
Merge branch (Please switch to branch master by git checkout master first)
$ git merge <br-name>
Save local changes to cache/stage for later use (save changes that you are not ready to commit)
$ git stash
List local changes to cache/stage
$ git stash list
Diff between a hash commit
$ git show a5915b6
$ git difftool HEAD^ HEAD
Diff cache/stage
$ git diff stash@{0}
$ git diff stash@\{0\} (Need a escape seq "\" in tcsh)
Get back local changes from cache/stage
$ git stash pop (stash@{0} will be remove)
$ git stash apply stash@{0} (stash@{0} will be remain)
Remove local changes from cache/stage
$ git stash drop stash@{0}
Convert cvs to git
[etienne@ubuntu /home/repo/git]$ sudo git cvsimport -d $CVSROOT -C proj.git -r cvs -k cvsprojrepo
[etienne@ubuntu ~/git]$ git clone file:///home/repo/git/proj.git/