git操作

来源:互联网 发布:金润雅 知乎 编辑:程序博客网 时间:2024/04/27 01:36
起步
1.初次运行git前的配置
git config --global user.name "Tsung" #设置用户名
git config --global user.email "username@email.com" #设置邮件
git config --global color.ui "always" #设置diff,status.branch等命令有颜色显示
git config --global core.editor emacs #设置文本编辑器为emacs,可以是vi/vim/
git config --global merge.tool vimdiff #设置差异分析工具,在解决合并冲突时会用
git config --global core.filemode false #设置忽略文件属性
git config --list #查看配置信息
git help <verb> #获取帮助
基础篇
2.1取得项目的git仓库
在工作目录中初始化新仓库
git init
git add *.c
git add README
git commit -m 'initial project version'
从现有仓库克隆
git clone [url]
2.1.记录每次更新到仓库
git status #检查当前文件状态
git add file #跟踪新文件
cat .gitignore #忽略某些文件
git diff #查看未暂存文件更新
git diff --cached #查看已暂存文件更新
git commit -m "commit log" #提交更新
git commit -am "commit log" #提交更新(跳过使用暂存区域)
git rm file #移除文件
git rm -f file #移除文件(文件已经到暂存区域里)
git rm --cached file #移除暂存区域,但仍保留在当前工作目录中。稍后可以在.gitignore中补上。
git mv file_from file_to #移动文件,相当于重命名。
2.2.查看提交历史
git whatchanged #查看有哪些文件改动
git log #列出所有的更新,最近的更新排列在最上面
git log [remote name]/[branch name] #查看远程分支提交记录
git log -p [commit-id] #查看某一commit-id提交记录
git log -p -<n> # -p选项显示每次提交的内容差异,-<n>则仅显示最近的n次更新,n可以是任何自然数
git log --stat #显示简要的增改行数统计
git log --pretty=oneline #提交放在一行显示
git log --since=2.weeks #列出所有两周的提交
git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
   --before="2008-11-01" --no-merges -- t/ #查看2008年10月期间,gitster提交的内容(位于项目t/目录下的文件)
2.3.撤销操作
git commit -m 'initial commit'
git add forgotten_file
git commit --amend #修改最后一次提交
#此三条命令最终产生一个提交,第二个提交命令修正了第一个的提交内容
git reset HEAD [file] #取消已经暂存的文件
git checkout -- [file] #取消对文件的修改
git reset --hard [commit-id] #回到过去某一个节点
git reset HEAD~1 #回到最后一个提交前的状态
2.4.远程仓库的使用
git remote -v #查看当前的远程库
git remote add [shortname] [url] #添加远程仓库
git fetch [remote-name] #从远程仓库抓取数据
git push [remote-name] [branch-name] #推送数据到远程仓库,克隆操作会自动使用默认的 master 和 origin 名字。
git remote show [remote-name] #查看远程仓库信息
git remote rename pb paul #修改某个远程仓库在本地的简称,比如想把pb改成paul
git remote rm paul #移除远程仓库paul
2.5.打标签
git tag #列显已有的标签
git tag -a v1.4 -m 'my version 1.4' #新建一个含附注类型的标签。-a指定标签名,-m指定标签说明
git tag -s v1.5 -m 'my signed 1.5 tag' #新建一个签署标签。
git tag v1.4-lw #新建一个轻量级标签
git show v1.4 #查看相应标签的版本信息
git tag -v [tag-name] #验证标签
git push origin [tagname] #分享标签
git push origin --tags #一次推送所有本地新增的标签
2.6.查看版本差异
git diff [commit1] [commit2] -- [file] #查看两个版本之间的差异
git diff [path] #查看工作版本和最后一次提交版本之间的差异
2.7.更新本地仓库
git pull
分支
3.1.分支的新建与合并
git branch [branch-name] #新建分支
git checkout -b [branch-name] #切换分支
git merge [branch-name] #合并分支(回到master分支下执行)
git status #查看合并情况
git mergetool #解决合并分支冲突的图形化工具
3.2分支的管理
git branch -v #查看各个分支最后一个提交对象的信息
git branch --merged #查看哪些分支已被并入当前分支
git branch --no-merged #查看尚未合并的工作
git branch -r #查看远程分支,也就是你git仓库里有哪些分支
git branch -a # 查看所有分支,远程和本地
3.3远程分支
git push (远程仓库名) (分支名) #推送本地分支
git checkout -b (分支名) (远程仓库名)/(分支名) #远程分支的内容合并到当前分支
git checkout --track [远程名]/[分支名] #跟踪远程分支
git push [远程名] :[分支名] #删除远程分支
3.4删除分支
git branch -D [branch-name] #删除分支(不论如何都删除)
git branch -d [branch-name] #删除分支TAG(分支合并到主干后才能删除)
git checkout [branch-name]#从其他分支切换到TAG分支
其他
4.1 git diff打包和解包
git diff > *.diff 打包成*.diff文件
有新添加文件时打包方法
git add *
git diff --cached > ../*.diff
patch -p1 < *.diff 恢复diff改动到工程中
4.2 git commit中加入换行符
git commit -m '1. this is the test 2. update file'
4.3本地全局忽略文件

git config --global core.excludesfile ~/.gitignore

4.4git cherry-pick用于把另一个本地分支的commit修改应用到当前分支
在本地master 分支上做了一个commit ( 38361a68138140827b31b72f8bbfd88b3705d77a ) , 如何把它放到 本地 old_cc 分支上?
$ git checkout old_cc
$ git cherry-pick 38361a68138140827b31b72f8bbfd88b3705d77a

4.5.撤销操作

git revert HEAD 撤销前一次 commit
git revert HEAD^ 撤销前前一次 commit
git revert commit (比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存。



参考文档:

Git-关于版本控制

Pro Git(中文版)

原创粉丝点击