git操作命令

jopen 9年前

配置

配置用户信息
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
配置文本编辑器
git config --global core.editor vim
配置差异分析工具-- kdiff3,tkdiff,meld,xxdiff,emerge,vimdiff,gvimdiff,ecmerge,opendiff
git config --global merge.tool vimdiff
参看配置信息
git config --list


帮助
git help <verb>
 
初始化
仓库初始化
git init
git add *.c
git add README

git commit -m 'initial project version'

git remote add <shortname> <url>

git push [remote-name] [branch-name]

从现有仓库克隆

git clone git://github.com/schacon/grit.git
git clone git://github.com/schacon/grit.git mygrit
 
 
参看
查看文件状态
git status
查看当前文件和暂存区差异,当前文件上次提交文件差异
git diff
git diff <file>
查看暂存文件和上次提交差异
git diff --cached
参看历史
git log
log可视化
gitk

 

 

提交
提交更新
git commit
跳过使用暂存区提交
git commit -a -m 'added new benchmarks'

 

 

撤销
撤销提交
git commit --amend
取消暂存
git reset HEAD <file>
取消对文件的修改
git checkout -- <file>
 
文件操作
移除文件
git rm <file>
只从跟踪清单中删除,文件不删除
git rm --cached readme.txt
移动文件
git mv file_from file_to
增加追踪文件
git add <file>
 
 
远程仓库
参看克隆的远程仓库
git remote -v
添加远程仓库(给远程路径取个别名)
git remote add [shortname] [url]
git remote add pb git://github.com/paulboone/ticgit.git
抓取远程仓库,不会合并
git fetch [remote-name]
git fetch pb
推送数据到远程仓库
git push [remote-name] [branch-name]
参看远程仓库信息
git remote show [remote-name]
更改仓库名称
git remote rename [old-name] [new-name]
git remote rename pb paul
 
 
分支
参看本地分支
git branch
查看远程分支
git branch -a
切换分支
git checkout <local-branch>
 
创建分支,从本地当前分支创建新的分支
git branch <new-local-branch>
创建分支,从本地其他分支创建新的分支
git branch <new-local-branch> <old-local-branch>
创建分支,从远程分支创建一个本地分支
git branch <new-local-branch> <remote>/<remote-branch>
创建并切换分支
git checkout -b <new-local-branch>
git checkout -b <new-local-branch> <old-local-branch>
git checkout -b <new-local-branch> <remote>/<remote-branch>
 
删除本地分支
git branch -d <local-branch>
删除远端分支
git push <remote-name> :<remote-branch>
 
合并本地其他分支到当前分支
git merge <local-branch>
合并远端到本地的当前分支
git pull [remote] <remote-branch>
 
创建远程分支,提交到远端分支

git push <remote> <local-branch>                   

取出我在本地的local-branch分支,推送到远程仓库的同名分支中去,远端无同名分支则创建同名分支

创建远程分支,提交到远端分支

git push <remote> <local-branch]:[remote-branch]   

取出我在本地的local-branch分支,推送到远程仓库的remote-branch分支中去,远端无remote-branch分支则创建remote-branch分支

 
设置本地分支跟踪远端分支
git branch --set-upstream-to=origin/远端分支 本地分支