Git 基本用法

git revert HEAD

git push origin HEAD:refs/for/master

 

from://http://sjtuecho.blog.163.com/blog/static/20501207420130119441404/

Git 基本用法  

2013-01-11 21:54:02|  分类: 默认分类|举报|字号 订阅

 
 

Basic:

$ git branch <new_branch>/<NULL> 创建分支/查看当前分支

$ git branch -d <branch> 删除分支(已经merge到其他分支的) 否则强制删除 -D

$ git checkout <branch> 检出分支

$ git checkout -b <new_branch> <tag>/<branch>/<index>/<NULL> 基于<*>创建新分支并检出

$ git add <file> $ git commit -a -m comments $ git status

$ git branch --merge 查看哪些分支已被并入当前分支

$ git log -p master..origin/master 显示 远程仓库的master分支的更改记录(从clone或者fetch起)

$ git config --list 显示当前配置

$ git tag -a <tag>

$ git grep "**" 不用checkou grep

$ git gc 压缩

一、本地与远程交互
本地分支(master)A <>  存储在本地的远程仓库副本(含远程的所有分支,用origin/master表示 ) B <> 远程仓库的分支C
$ git remote -v  # 显示已经有的远程仓库
koke      git://github.com/koke/grit.git   #只能get
origin    git@github.com:mojombo/grit.git  #这个用的是ssh格式, 我自己可以推送的仓库
$ git remote add origin https://EchoYan@bitbucket.org/EchoYan/_depositary_name.git
$ git clone origin # 复制远程仓库到本地, 存为本地副本, 与本地自己的仓库不是一回事
# 添加远程仓库,并别名为origin,默认仓库
# clone 自动创建本地master 分支来track远程仓库的master分支


cd /path/to/my/repo git remote add origin https://Ericales@bitbucket.org/Ericales/vimrc.git git push -u origin --all # pushes up the repo and its refs for the first time git push -u origin --tags # pushes up any tags

$ git fetch origin  #获取远程仓库的所有更新, 应用到远程仓库的本地副本上(针对所有分支),不是应用到自己的分支上。
# 即 把C 的更新应用到 B

$ git merge origin/branch_name  #把远程仓库branch merge到自己当前所在的分支上

# 即 把 B 的更新 应用到 A

# 我们不能编辑本地的远程分支副本,因此要把远程分支和本地分支关联(track),
修改本地自己的,然后推送到远程
## 以下两种方式就track了
$ git checkout -b new_local_branch origin/remote_branch # 根据远程仓库的分支来 创建新本地分支 C => A
$ git push origin <local_branch_name>:<remote_branch_name> # 根据本地的分支 创建远程仓库上的分支 A => C
# 这两种方式下的分支是相互关联的分支,所谓跟踪分支(tracking branch),彼此对应
# push 是用本地的去更新远程的, 而pull是用远程的去更新本地的

$ git push origin <branch_name> # 用本地的分支 去更新远程中的关联分支(tracking branch)
$ git pull origin <branch_name> # 用远程仓库中与本地master跟踪的分支(即仓库中的master分支)去 更新本地的master分支
## 无参数的命令git push 与git pull会将当前分支与其tracking branch同步更新
$ git push origin :develop #删除远程分支develop

$ git remote show origin # 查看某个远程仓库的详细信息
$ git remote rename <old> <new> # 修改 远程仓库的alias
git remote rm <depositary_name> # 删除远程仓库
$ git tag -d <tag_name> #删除本地tag 
$
git push --tags #推送本地所有新增的标签tag
$ git push origin <tag_name> # 推送本地某一个标签
$ git push origin :refs/tags/<tag_name> #删除远程仓库中的标签tag_name
#关于submodule:
$ git submodule init
$ git update 
才能更新
二、本地仓库的检出、提交和撤销

$git rm -n <file_name> # 查看某一文件是否被跟踪

$git rm --cached readme.txt # 文件放弃跟踪,但是还是保留在工作目录中。 没有--cached 选项,就完全删除文件。

# 如果直接rm file, git status 会提示有文件修改但没存。

// 修改最近的一次提交, 包括comments
$ git commit -m 'initial commit' $ git add forgotten_file $ git commit --amend
最近的提交A < > 暂存区修改 B <> 工作目录新修改C

检出:

$ git checkout branch_name tag_name #取指定分支branch_name的tag_name的版本
$ git checkout commit_id  file_name  #取文件file_name的 在commit_id是的版本。commit_id为 git commit 时的sha值。
  

撤销: 
$ git reset --hard HEAD  # 放弃暂存区B和工作目录C的所有修改,恢复最近一次提交状态 (A ->C 所有文件)
$ git checkout -- <file_name>  #恢复某文件到最近一次提交状态,放弃checkout后的修改 (A -> C 指定文件)
$ git revert HEAD     #撤消最近的一个提交 (针对已经的commit,跟B和C无关)
$ git revert HEAD^    # 撤消上次”(next-to-last)的提交 (^ 个数可以递增)
差异比较
@  你通常用git diff来找你当前工作目录和上次提交与本地索引间的差异。

$ git diff #(比较B,C)

上面的命令会显示在当前的工作目录里的,没有 staged(添加到索引中),且在下次提交时 不会被提交的修改。
@ 如果你要看在下次提交时要提交的内容(staged,添加到索引中)。 命令会显示你当前的索引和上次提交间的差异;这些内容在不带"-a"参数运行 "git commit"命令时就会被提交。

$ git diff --cached #(比较A,B)

@ 这条命令会显示你工作目录与上次提交时之间的所有差别,这条命令所显示的 内容都会在执行"git commit -a"命令时被提交。

$ git diff HEAD (比较A,C

比较分支间差异

$ git diff <branch1>..<branch2>

忽略某些文件
项目中经常会生成一些Git系统不需要追踪(track)的文件。典型的是在编译生成过程中 产生的文件或是编程器生成的临时备份文件。当然,你不追踪(track)这些文件,可以 平时不用"git add"去把它们加到索引中。 但是这样会很快变成一件烦人的事,你发现 项目中到处有未追踪(untracked)的文件; 这样也使"git add ." 和"git commit -a" 变得实际上没有用处,同时"git status"命令的输出也会有它们。
你可以在你的顶层工作目录中添加一个叫".gitignore"的文件,来告诉Git系统要忽略 掉哪些文件,下面是文件内容的示例:

# 以'#' 开始的行,被视为注释.
# 忽略掉所有文件名是 foo.txt 的文件.
foo.txt
# 忽略所有生成的 html 文件,
*.html
# foo.html是手工维护的,所以例外.
!foo.html
# 忽略所有.o 和 .a文件.
*.[oa]

你可以点这里gitignore 查看一下详细的语法解释. 你也可以把".gitignore" 这个文件放到工作树(working tree)里的其它目录中,这就会在它和它的子目录起忽略(ignore) 指定文件的作用。.gitignor文件同样可以像其它文件一样加到项目仓库里( 直接用git add .gitignore 和git commit等命令), 这样项目里的其它开发者也能共享同一套忽略 文件规则。
如果你想忽略规则只对特定的仓库起作用,你可以把这些忽略规则写到你的仓库下 .git/info/exclude文件中,或是写在Git配置变量core.excludesfile中指定的 文件里。有些Git命令也可在命令行参数中指定忽略规则,你可以在这
里:gitignore 查看详细的用法。

 关于rebase
 和merge的区别

做完之后 二者得到的是一样的,但是如果我们想恢复,那那么log中的历史节点是不一样的。 
例子: 

$git checkout master # orogin O

/* file is begin */

$git checkout -b develop master #

$git commit -a -m "commit A"

/* file is begin */

A

$git checkout master

$git commit -a -m "commit B"

/* file is begin */

 

B

如果我们

$git merge develop # 之后记为LATEST L

/* file is begin */

A

B

log中存的是这样的:

O ---->A----------> L

       |----->B---|

那么我们能恢复到A状态,也可以恢复到B状态。

但如果

$ git rebase develop

log中只有一条线了

O---> A-->B ==L

如果$git revert HEAD 只能得到的 

/* file is begin */

A

如果$git revert HEAD^  只能得到的 

/* file is begin */

效果和merge是一样的,但是log中是不一样的。 区别就是rebase先将mywork 中我自己的新commit删除并 暂存起来, 再去把origin中的他人新commit 加到mywork, 然后再去commit 刚才 暂存的mywork上自己的commit。 而merger 直接是讲 origin上的别人的commit合并到我自己的本地分支。 历史中保存各自的commit节点。 rebase之后,我自己的提交的时间点被更新了, 而merge没有。

$ git checkout -b mywork origin/master #从远程创建自己的本地分支
# 在我的本地分支mywork做一些自己的commit, 同时远程仓库有其他人在commit,不会体现在我的本地分支上
$ git checkout mywork
$ git rebase origin # 我的本地分支同步到跟origin现在的状态一致
# (他人的新commit体现在我本地分支,我自己的commit被删除了,但留有备份), 再把我在本地的commit重新提交到mywork。

$ git checkout -b mywork origin/master #从远程创建自己的本地分支
# 在我的本地分支mywork做一些自己的commit, 同时远程仓库有其他人在commit,不会体现在我的本地分支上
$ git checkout mywork
$ git merge origin #将 在我checkout节点之后,远程仓库中的新的commit 合并到我自己的本地分支上。

删除分支如何恢复

$ git fsck --lost-found #找出删除的分支中的commit的index

$ git show <index> #查看index对应的commit的详细信息

$ git rebase/merge <index> #恢复这个commit

 

posted @ 2014-10-29 10:08  wanqi  阅读(1273)  评论(0编辑  收藏  举报