========================
(1)在Linux上安装git
$ sudo yum install git
$ sudo apt-get install git
========================
(2)在Windows上安装git
http://windows.github.com
========================
(3)下载git源代码
git clone git://git.kernel.org/pub/scm/git/git.git
========================
(4)查看帮助
$ git help <verb>
$ git <verb> --help
$ man git-<verb>
例子:
$ git help config
$ git help log
========================
(5)配置你的身份标识
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
注:可以不加--global来给相应应的项目进行配置
$ git config user.name "John Doe"
========================
(6)配置编辑器为vim
$ git config --global core.editor vim
========================
(7)配置合并工具
$ git config --global merge.tool vimdiff
========================
(7)查看配置
$ git config -l
========================
(8)初始化一个仓库
$ git init
注:在空目录下执行
========================
(9)克隆一个仓库
$ git clone [url]
注:可以clone本地或者远程仓库
========================
(10)remote操作
#查看远程主机
$ git remote
$ git remote -v
#添加远程主机
$git remote add [shortname] [url]
例子:
$git remote add newhost git://github.com/paulboone/ticgit.git
$git fetch newhost
========================
(11)添加跟踪文件
$ git add [path/file]
例子:
$ git add test.c
$ git add project/
$ git add .
========================
(12)查看文件修改状态
$ git status
例子:
git status
git status .
git status project/
========================
(13)代码本地提交
$ git commit
例子:
$ git commit -m "Story 182: Fix benchmarks for speed" #需要先git add修改的文件
$ git commit -a -m 'added new benchmarks'
$ git commit --amend #修订最后一次提交,修订前git add对应修订文件
========================
(14)撤销某次提交的修改
$ git revert [sha1]
========================
(15)查看差异
$ git diff
例子:
$ git diff
#查看被git add的文件差异
$ git diff --cached 或者 git diff --staged
#
$ git diff [branch1] [branch2]
#查看被修改的文件
$ git diff [branch1] [branch2] --name-status 或者 --stat
$ git diff [sha1] [sha2]
$ git diff [sha1]...[sha2]
$ git diff [tag1] [tag2]
========================
(16)删除操作
$ git rm [file/-r dir]
========================
(17)清除临时文件
$ git clean -df
========================
(18)重命名
$ git mv [fromfile1] [tofile2]
========================
(19)查看日志
$ git log
例子:
$ git log
$ git log -p -2
$ git log --stat
$ git log --name-status
$ git log --pretty=oneline
$ git log --pretty=format:"%h - %an, %ar : %s"
$ git log --pretty=format:"%h %s" --graph
其他option:
--since, --after
--until, --before
--author
--committer
--grep
========================
(20)查看单次提交修改
#查看最后一次提交
$ git show
$ git show [branch]
#查看对应sha1或tag的修改
$ git show [sha1/tag]
========================
(21)查看文件每一行代码是谁修改的
$ git blame [filename]
#查看160~169之间
$ git blame -L 160,+10 sha1_file.c
========================
(22)reset操作
$ git reset
例子:
$ git reset HEAD
$ git reset [file/path/sha1]
$ git reset --hard [sha1]
========================
(23)checkout操作
$ git checkout
例子:
$ git checkout -- .
$ git checkout -- [file]
#从当前分支checkout到本地分支local_branch2
$ git checkout local_branch2
#基于远程分支remote_branch1,创建一个本地分支newbranch1并checkout到newbranch1
$ git checkout -b newbranch1 remote_branch1
#强制切换到分支
========================
(24)储藏操作
#保存stash,可以保存多次stash
$ git stash
#查看最后一次stash
$ git stash show
#列出所有stash
$ git stash list
#提取出stash
$ git stash pop
#丢弃stash 谨慎使用
$ git stash drop
例子:
$ git stash list
stash@{0}: WIP on zjywkbranch: 0c96a43 xxx
stash@{1}: WIP on zjywkbranch: 0c96a43 xxx
$ git stash pop
$ git stash pop
========================
(25)branch操作
#查看本地分支
$ git branch
#查看所有分支
$ git branch -a
#基于当前分支,新建分支newbh1
$ git branch [newbh1]
#删除分支
$ git branch -d testing
#强制删除分支
$ git branch -D testing
========================
(26)fectch操作
$ git fetch <远程主机名> <分支名>
例子:
$ git fetch
$ git fetch origin
========================
(27)pull操作
$ git pull <远程主机名> <远程分支名>:<本地分支名>
例子:
$ git pull
$ git pull origin
========================
(28)push操作
$ git push <远程主机名> <本地分支名>:<远程分支名>
例子:
$ git push origin HEAD:refs/for/hip/titanium_5.0_int_r4240
========================
(29)merge操作
$ git merge [branch]
$ git merge --squash
例子:
#merge brach2上的修改到当前分支
$ git merge branch2
#merge其他分支上我们自己的修改到当前分支
$ git merge -s ours LNX.LA.3.5.2.2-02110-8x74.0
$ git merge 4.4.3-S01-003-RU1.0.3.57a
========================
(30)cherry-pick操作
$ git cherry-pick [sha1]
$ git cherry-pick --continue
$ git cherry-pick --quit
$ git cherry-pick --abort
========================
(31)rebase操作
$ git rebase [branch]
#交互式rebase
$ git rebase -i [sha1/branch]
# Rebase da867ed..da867ed onto da867ed
#
# Commands:
# p, pick = use commit (使用提交)
# r, reword = use commit, but edit the commit message (使用提交,但编辑提交信息)
# e, edit = use commit, but stop for amending (停下来编辑提交)
# s, squash = use commit, but meld into previous commit (使用提交,但与前一提交合并)
# f, fixup = like "squash", but discard this commit's log message (使用提交,但与前一提交合并,并丢弃本次提交日志信息)
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom. (可以调整提交顺序,执行顺序从上到下)
#
# If you remove a line here THAT COMMIT WILL BE LOST. (删除行的提交将会丢失)
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
例子:
#把tag LNX.LA.3.5.2.2-02110-8x74.0和4.4.3-S01-003-RU1.0.3.57a之间的提交,一个一个的应用到当前分支上
$ git rebase LNX.LA.3.5.2.2-02110-8x74.0 4.4.3-S01-003-RU1.0.3.57a --onto stcr4240base
$ git rebase --continue
$ git rebase --skip
$ git rebase --abort
========================
(32)标签操作
#查看标签
$ git tag
#查看标签
$ git tag -l 'v1.8.5*'
#创建轻量级标签
$ git tag v1.4-lw
#创建标签 -m加注释
$ git tag -a v1.4 -m 'my version 1.4'
========================
(33)patch操作
#打patch
$ git apply [patchfile]
$ patch -p1 < xxx.patch
#打patch并提交
$ git am [patchfile]
$ git am -3 [patchfile]
$ git am --reject [patchfile]
#中止am
$ git am --abort
#批量打patch时,跳过某次am
$ git am --skip
#解决某次am失败的冲突
$ git am --resolved
#制作sha1之后所有提交的patch
$ git format-patch [sha1]
#制作一个sha1对应的patch
$ git format-patch -1 [sha1]
#制作sha1和sha1之前n-1次提交的patch
$ git format-patch -n [sha1]
#制作两个提交之前的patch
$ git format-patch [sha1]..[sha2]
========================
(34)解冲突合并工具
$ git mergetool
========================
(35)别名
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
========================
(36)仓库里查找
$ git grep [string]
========================
(37)图形化工具
gitk & gitg
========================
(38)repo操作
usage: repo COMMAND [ARGS]
The complete list of recognized repo commands are:
abandon Permanently abandon a development branch
branch View current topic branches
branches View current topic branches
checkout Checkout a branch for development
cherry-pick Cherry-pick a change.
commit
cpfg cherry pick from gerrit
cpto cherry pick from gerrit and upload to gerrit
diff Show changes between commit and working tree
download Download and checkout a change
forall Run a shell command in each project
grep Print lines matching a pattern
help Display detailed help on a command
info Get info on the manifest branch, current branch or unmerged branches
init Initialize repo in the current directory
list List projects and their associated directories
manifest Manifest inspection utility
overview Display overview of unmerged project branches
prune Prune (delete) already merged topics
rebase Rebase local branches on upstream branch
selfupdate Update repo to the latest version
smartsync Update working tree to the latest known good revision
stage Stage file(s) for commit
start Start a new branch for development
status Show the working tree status
sync Update working tree to the latest revision
upload Upload changes for code review
version Display the version of repo
#查看帮助
$ repo help
$ repo help <verb>
$ repo <verb> --help
例子:
$ repo help init
$ repo init --help
$ repo init --reference=[dir] -u [url] -b branch -m manifest-name.xml
$ repo sync
#批处理命令
$ repo forall
#为所有仓库的远程分支remote_bh_a创建本地分支local_bh_b
$ repo forall -c "git checkout -b local_bh_b remote_bh_a"