码迷,mamicode.com
首页 > 其他好文 > 详细

Git命令收集

时间:2018-08-28 17:52:56      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:strong   --   seconds   不一致   模糊匹配   erro   Fix   而且   opera   

记录一些比较特别的命令:

1.清理恢复

git clean -xdf

清理所有未untracked的文件和文件夹

git reset --hard

恢复所有已tracked的文件和文件夹

将这两个合并起来就是恢复到旧的干净的版本。

删除所有未untracked的文件和文件夹,但是不删除在.gitignore中的文件或文件夹

git clean -df

删除之前不知道删除什么文件,可以加-n,比如:

git clean -ndf

2.回滚,reset与revert的区别

git reset --hard 4971ebc

git reset回滚到某个版本,只是移动指针,可以向前,也可以向后。


git revert就不一样,它是创建一个新的commit,来覆盖旧的commit,指针只往后移动

λ git revert head
[master 67d4613] Revert "delete abc.txt"
 2 files changed, 8 insertions(+)
 create mode 100644 ABc.txt
 create mode 100644 abc.txt

回滚到指定版本:

λ git revert 5ccc4db
[master 9149046] Revert "删除大小写的诡异"
 4 files changed, 32 insertions(+)
 create mode 100644 File1.txt
 create mode 100644 Readme.txt

查看两次revert的日志记录:

* 9149046 - (HEAD -> master) Revert "删除大小写的诡异" (41 seconds ago) | hongdada
* 67d4613 - Revert "delete abc.txt" (7 minutes ago) | hongdada
* 906417c - (origin/master) delete abc.txt (14 minutes ago) | hongdada
* 5ccc4db - 删除大小写的诡异 (14 minutes ago) | hongdada

所以一般在工作中,如果线上代码发生冲突,要用git revert来回滚,并签入到远程服务器,

因为用git reset,在签入远程服务器时会发现全是冲突,因为远程服务器里面最近的commitid在本地不存在。

如果实在有把握,可以git reset回到某个版本,并强制推送远程服务器

git push -f

3.merge,rebase,cherry-pick区别

git merge --squash dev 
git cm "..."

merge --squash 是合并dev分支中的commit,再添加描述提交

git rebase dev
git rebase --continue
git rebase --abort
git rebae --skip

在rebase的过程中,也许会出现冲突(conflict). 在这种情况,Git会停止rebase并会让你去解决冲突;

在解决完冲突后,用"git-add"命令去更新这些内容的索引(index), 然后,你无需执行 git-commit,只要执行 git rebase --continue来继续

或者根本不解决冲突,调用git rebase --skip 将引起冲突的commits丢弃掉

这样git会继续应用(apply)余下的补丁。在任何时候,你可以用git rebase --abort参数来终止rebase的行动。

git cherry-pick  commitid

cherry-pick可以对整个项目中的某个commitid进行合并,不仅可以用在不同分支之间, 还可以用在同一个分支上,不同分支的用法如上所述. 同一分支用法也是一样的, 同一分支使用情形:

比如说你在某一个向某个分支中添加了一个功能, 后来处于某种原因把它给删除了_,_然而后来某一天你又要添加上这个功能了, 这时候就可以使用cherry-pick把添加那个功能的commit

4.删除不存在对应远程分支的本地分支

D:\Development\lpapi.rongzi.comn (master)
λ git b -a
  developer
  hongqi * master
  remotes/origin/HEAD -> origin/master
  remotes/origin/developer
  remotes/origin/hongqi
  remotes/origin/master
  remotes/origin/newdev

D:\Development\lpapi.rongzi.comn (master)
λ git remote prune origin newdev
Pruning origin
URL: http://10.40.3.31/rzr.net/lpapi.rongzi.com.git
 * [pruned] origin/newdev
fatal: 'newdev' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

远程已不存在分支newdev,但是本地还是存在,所以删除本地对应的远程分支

具体的操作:

D:\Development\Rongzi.RZR.M (master)                                               
λ git b -a                                                                         
  bugfix                                                                           
  hongqi                                                                           
* master                                                                           
  remotes/origin/Develper                                                          
  remotes/origin/HEAD -> origin/master                                             
  remotes/origin/bugfix                                                            
  remotes/origin/develop                                                           
  remotes/origin/developer                                                         
  remotes/origin/feature                                                           
  remotes/origin/hongqi                                                            
  remotes/origin/master                                                            
                                                                                   
D:\Development\Rongzi.RZR.M (master)                                               
λ git remote show origin                                                           
* remote origin                                                                    
  Fetch URL: http://10.40.3.31/rzr.net/Rongzi.RZR.M.git                            
  Push  URL: http://10.40.3.31/rzr.net/Rongzi.RZR.M.git                            
  HEAD branch: master                                                              
  Remote branches:                                                                 
    Develper                    tracked                                            
    developer                   tracked                                            
    feature                     tracked                                            
    master                      tracked                                            
    refs/remotes/origin/bugfix  stale (use 'git remote prune' to remove)           
    refs/remotes/origin/develop stale (use 'git remote prune' to remove)           
    refs/remotes/origin/hongqi  stale (use 'git remote prune' to remove)           
  Local branches configured for 'git pull':                                        
    hongqi merges with remote hongqi                                               
    master merges with remote master                                               
  Local ref configured for 'git push':                                             
    master pushes to master (up to date)                                           
                                                                                   
D:\Development\Rongzi.RZR.M (master)                                               
λ git remote prune origin                                                          
Pruning origin                                                                     
URL: http://10.40.3.31/rzr.net/Rongzi.RZR.M.git                                    
 * [pruned] origin/bugfix                                                          
 * [pruned] origin/develop                                                         
 * [pruned] origin/hongqi                                                          
                                                                                   
D:\Development\Rongzi.RZR.M (master)                                               
λ git b -a                                                                         
  bugfix                                                                           
  hongqi                                                                           
* master                                                                           
  remotes/origin/Develper                                                          
  remotes/origin/HEAD -> origin/master                                             
  remotes/origin/developer                                                         
  remotes/origin/feature                                                           
  remotes/origin/master

5.git pull,git push 报错(本地分支没有对应的远程分支)


D:\Development\Rongzi.RZR.MI (develop)
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> develop

D:\Development\Rongzi.RZR.MI (develop)
$ git branch -vv * develop                  bd19efd fengsheng
  feature/RZRAPP           427ea9c [origin/feature/RZRAPP] Merge branch 'develop'
  feature/ThirdCooperation ebbe5e8 [origin/feature/ThirdCooperation] 金额单位修改
  hongqi                   cbb9edd [origin/hongqi: gone] modify mojie channelId
  master                   39a624f [origin/master: behind 1] fix bug“

使用git branch -vv 可以查看本地分支和远程分支的关联关系

使用命令 git branch --set-upstream-to=origin/远程分支的名字 本地分支的名字

D:\Development\Rongzi.RZR.MI (develop)
$ git branch --set-upstream-to=origin/develop develop
Branch develop set up to track remote branch develop from origin.

D:\Development\Rongzi.RZR.MI (develop)
$ git pull
Already up-to-date.

D:\Development\Rongzi.RZR.MI (develop)
$ git branch -vv * develop                  bd19efd [origin/develop] fengsheng
  feature/RZRAPP           427ea9c [origin/feature/RZRAPP] Merge branch 'develop'
  feature/ThirdCooperation ebbe5e8 [origin/feature/ThirdCooperation] 金额单位修改
  hongqi                   cbb9edd [origin/hongqi: gone] modify mojie channelId
  master                   39a624f [origin/master: behind 1] fix bug“

也可以使用简化版本

git branch --set-upstream-to=origin/develop develop

(简化版-此命令的含义是,是指当前分支的upstream为origin远程仓库的develop分支)
git b -u origin/develop

6.对远程分支的操作:

在远程生成本地分支对应的远程分支

$ git push -u origin feat
$ git push -u origin feat:feat

$ git b -u origin/feat
$ git b -u origin/feat feat 
(注意这里为b,而且名称必须origin/远程名称)

git b.... :当前分支的upstream为origin远程仓库的dev分支,(必须已经存在对应的远程分支)

git push....:推送feat分支到远程origin仓库feat分支,并且建立本地分支feat的upstream为origin/feat(远程可不存在,如果存在,要看commit版本,可能会有冲突)

删除:(冒号方式的删除,原理是,冒号左边应该为本地分支,右边为远程分支,现在推送了一个空的本地分支到远程分支)

删除远程dev分支,分别是2种不同的命令
$ git push origin :dev
$ git push origin -d dev

$ git push origin --delete dev

取消upstream:

取消当前分支的upstream
$ git branch --unset-upstream
取消其他分支的upstream
$ git branch --unset-upstream [分支名]

7.模糊匹配:

查看某文件修改

git diff *House*

撤销某文件修改

git co *House*

** 8.error: src refspec hongqi does not match any.**

出现上述问题时,执行下面的命令进行推送,一般是因为本地分支名称与远程分支名称不一致导致。

$ git push origin HEAD:hongqi

http://www.cnblogs.com/sky-heaven/p/6000494.html

https://blog.zengrong.net/post/1746.html

http://blog.csdn.net/xhl_will/article/details/8450193

http://www.open-open.com/news/view/1b81290

http://higoge.github.io/2015/07/06/git-remote03/

https://blog.zengrong.net/post/1746.html

http://www.ruanyifeng.com/blog/2014/06/git_remote.html

Git命令收集

标签:strong   --   seconds   不一致   模糊匹配   erro   Fix   而且   opera   

原文地址:https://www.cnblogs.com/hongdada/p/9549506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!