git

来源:互联网 发布:知客是什么工作 编辑:程序博客网 时间:2024/06/04 18:57

经常发现好多时候修改线上代码的时候找不到原仓库地址了,头疼啊,还是git够强大,下面介绍下方法

可以通过命令 git remote show [remote-name] 查看某个远程仓库的详细信息,比如要看所克隆的 origin 仓库,可以运行:

$ git remote show origin* remote origin  URL: git://github.com/schacon/ticgit.git  Remote branch merged with 'git pull' while on branch master    master  Tracked remote branches    master    ticgit

除了对应的克隆地址外,它还给出了许多额外的信息。它友善地告诉你如果是在 master 分支,就可以用git pull 命令抓取数据合并到本地。另外还列出了所有处于跟踪状态中的远端分支。

上面的例子非常简单,而随着使用 Git 的深入,git remote show 给出的信息可能会像这样:

$ git remote show origin* remote origin  URL: git@github.com:defunkt/github.git  Remote branch merged with 'git pull' while on branch issues    issues  Remote branch merged with 'git pull' while on branch master    master  New remote branches (next fetch will store in remotes/origin)    caching  Stale tracking branches (use 'git remote prune')    libwalker    walker2  Tracked remote branches    acl    apiv2    dashboard2    issues    master    postgres  Local branch pushed with 'git push'    master:master

它告诉我们,运行 git push 时缺省推送的分支是什么(译注:最后两行)。它还显示了有哪些远端分支还没有同步到本地(译注:第六行的 caching 分支),哪些已同步到本地的远端分支在远端服务器上已被删除(译注:Stale tracking branches 下面的两个分支),以及运行 git pull 时将自动合并哪些分支(译注:前四行中列出的 issues 和 master 分支)。

git 删除本地分支和远程分支、本地代码回滚和远程代码库回滚

【git 删除本地分支】

git branch -D br

 

【git 删除远程分支】

git push origin :br  (origin 后面有空格)

 

git代码库回滚: 指的是将代码库某分支退回到以前的某个commit id

【本地代码库回滚】:

git reset --hard commit-id :回滚到commit-id,讲commit-id之后提交的commit都去除

git reset --hard HEAD~3:将最近3次的提交回滚

 

【远程代码库回滚】:

这个是重点要说的内容,过程比本地回滚要复杂

应用场景:自动部署系统发布后发现问题,需要回滚到某一个commit,再重新发布

原理:先将本地分支退回到某个commit,删除远程分支,再重新push本地分支

操作步骤:

1、git checkout the_branch

2、git pull

3、git branch the_branch_backup //备份一下这个分支当前的情况

4、git reset --hard the_commit_id //把the_branch本地回滚到the_commit_id

5、git push origin :the_branch //删除远程 the_branch

6、git push origin the_branch //用回滚后的本地分支重新建立远程分支

7、git push origin :the_branch_backup //如果前面都成功了,删除这个备份分支

如果使用了gerrit做远程代码中心库和code review平台,需要确保操作git的用户具备分支的push权限,并且选择了 Force Push选项(在push权限设置里有这个选项)

另外,gerrit中心库是个bare库,将HEAD默认指向了master,因此master分支是不能进行删除操作的,最好不要选择删除master分支的策略,换用其他分支。如果一定要这样做,可以考虑到gerrit服务器上修改HEAD指针。。。不建议这样搞



原创粉丝点击