Git 命令收集

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

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/newdevD:\Development\lpapi.rongzi.comn (master)λ git remote prune origin newdevPruning originURL: http://10.40.3.31/rzr.net/lpapi.rongzi.com.git * [pruned] origin/newdevfatal: 'newdev' does not appear to be a git repositoryfatal: Could not read from remote repository.Please make sure you have the correct access rightsand 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

9.取消某次合并

git merge --abort #如果Git版本 >= 1.7.4git reset --merge #如果Git版本 >= 1.6.1

**10.暂存命令stash使用 **

git stash #将本地修改暂时存储起来git stash list #查看暂存的信息git stash pop  #应用最近一次暂存的内容git stash apply stash@{1} #应用指定版本的暂存内容git stash clear  #清空暂存栈

11.git revert撤销merge

正常方式revert merge的commitid:

$ git revert 98ddcacerror: commit 98ddcace161b532103597f5eaa4b9ac8b873ca1c is a merge but no -m option was given.fatal: revert failed

log:

*   g - Merge branch 'dev' (30 minutes ago) | hongda
|\
| * e - (dev) 3333 (45 minutes ago) | hongda
| * d - 1111 (45 minutes ago) | hongda
* | f - 4444 (52 seconds ago) | hongda
|/
* c - (origin/master, origin/HEAD) 取消注释 (1 year, 3 months ago) | hongda
* b - 调整后台调用的接口,修改分页 (1 year, 3 months ago) | hongda
* a - 修改 (1 year, 3 months ago) | hongdada

可以看到它merge是两个commitid,分别是它整个提交的commitid的起始

a -> b -> c -> f -- g -> h (master)
           \      /
            d -> e  (dev)

上图中,g 是 merge commit,其他的都是常规 commit。g 的两个 parent 分别是 fe

在你合并两个分支并试图撤销时,Git 并不知道你到底需要保留哪一个分支上所做的修改。从 Git 的角度来看,master 分支和 dev 在地位上是完全平等的,只是在 workflow 中,master 被人为约定成了「主分支」。

于是 Git 需要你通过 mmainline 参数来指定「主线」。merge commit 的 parents 一定是在两个不同的线索上,因此可以通过 parent 来表示「主线」。m 参数的值可以是 1 或者 2,对应着 parent 在 merge commit 信息中的顺序。

$ git show 98ddcac
commit 98ddcace161b532103597f5eaa4b9ac8b873ca1c
Merge: d9090a1 f032a1b
Author: hongda <hongda159505@qq.com>
Date:   Wed Dec 5 16:39:23 2018 +0800

    Merge branch 'dev'

那么,$ git revert g -m 1 将会保留 master 分支上的修改,撤销 dev 分支上的修改。

12.git pull冲突

λ git pull
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

  git config pull.rebase false  # merge (the default strategy)
  git config pull.rebase true   # rebase
  git config pull.ff only       # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

Already up to date.

该问题就是,我本地有commit,但是未提交,origin也有新的commit,并且冲突

如果这里使用rebase为true,就在pull的时候一步步合并代码,并生成新的commit再提交

目前还是使用merge比较好,全局设置

git config --global pull.rebase false

查看:

λ git config --global --listuser.name=hongdauser.email=hongda159505@qq.comalias.last=log -1 --stat。。。。pull.rebase=false

13.git换行符问题

git add .fatal: CRLF would be replaced by LF ...gcc "获取产品大类列表"fatal: CRLF would be replaced by LF in ....java

文本文件所使用的换行符,在不同的系统平台上是不一样的

UNIX/Linux 使用的是 0x0A(LF),早期的 Mac OS 使用的是 0x0D(CR),后来的 OS X 在更换内核后与 UNIX 保持一致了。但 DOS/Windows 一直使用 0x0D0A(CRLF) 作为换行符。

git提供了一个 autocrlf 的配置项,用于在提交和检出时自动转换换行符,该配置有三个可选项:

  • true: 提交时转换为 LF,检出时转换为 CRLF
  • false: 提交检出均不转换
  • input: 提交时转换为LF,检出时不转换

用如下命令即可完成配置:

# 提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true

# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input

# 提交检出均不转换
git config --global core.autocrlf false

如果把 autocrlf 设置为 false 时,那另一个配置项 safecrlf 最好设置为 ture。该选项用于检查文件是否包含混合换行符,其有三个可选项:

  • true: 拒绝提交包含混合换行符的文件
  • false: 允许提交包含混合换行符的文件
  • warn: 提交包含混合换行符的文件时给出警告

配置方法:

# 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true

# 允许提交包含混合换行符的文件
git config --global core.safecrlf false

# 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

目前采取的方法是,idea中修改换行符为LF,并git全局设置:

$ git config --global core.autocrlf false
$ git config --global core.safecrlf false

这样设置最宽松。

14.修改git push的默认行为

我们可以通过git version确定当前的git版本(如果小于2.0,更新是个更好的选择),通过git config --global push.default 'option'改变push.default的默认行为(或者也可直接编辑~/.gitconfig文件)。

push.default 有以下几个可选值:

其用途分别为:

  • nothing - push操作无效,除非显式指定远程分支,例如git push origin develop(我觉得。。。可以给那些不愿学git的同事配上此项)。
  • current - push当前分支到远程同名分支,如果远程同名分支不存在则自动创建同名分支。
  • upstream - push当前分支到它的upstream分支上(这一项其实用于经常从本地分支push/pull到同一远程仓库的情景,这种模式叫做central workflow)。
  • simple - simple和upstream是相似的,只有一点不同,simple必须保证本地分支和它的远程 upstream分支同名,否则会拒绝push操作。
  • matching - push所有本地和远程两端都存在的同名分支,push操作就本地所有分支提交到远程。

因此如果我们使用了git2.0之前的版本,push.default = matching,git push后则会推送当前分支代码到远程分支,而2.0之后,push.default = simple,如果没有指定当前分支的upstream分支,就会收到上文的fatal提示。

λ git config --global push.default current

λ git config --list |grep push
alias.ps=push origin master
push.default='current'

15.cherry-pick多个commit到其他分支

git cherry-pick命令的参数,不一定是提交的哈希值,分支名也是可以的,表示转移该分支的最新提交。

git cherry-pick feature

Cherry pick 支持一次转移多个提交。

git cherry-pick <HashA> <HashB>

上面的命令将 A 和 B 两个提交应用到当前分支。这会在当前分支生成两个对应的新提交。

如果想要转移一系列的连续提交,可以使用下面的简便语法。

git cherry-pick A..B 

上面的命令可以转移从 A 到 B 的所有提交。它们必须按照正确的顺序放置:提交 A 必须早于提交 B,否则命令将失败,但不会报错。

注意,使用上面的命令,提交 A 将不会包含在 Cherry pick 中。如果要包含提交 A,可以使用下面的语法。

git cherry-pick A^..B 

16..gitignore忽略target无效

git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法.
但是有时候在项目开发过程中,突然心血来潮想把某些目录或文件加入忽略规则,按照上述方法定义后发现并未生效,原因是.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。那么解决方法就是先把本地缓存删除(改变成未track状态),然后再提交:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

17.git代理配置

git config --global http.proxy http://127.0.0.1:1080git config --global https.proxy https://127.0.0.1:1080

只针对github:

git config --global http.https://github.com.proxy socks5://127.0.0.1:1080git config --global https.https://github.com.proxy socks5://127.0.0.1:1080

取消代理:

git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

or:

git config --global --unset http.proxy
git config --global --unset https.proxy

也可修改.gitconfig配置

[url "https://"]   
    insteadOf = git://
[http "https://github.com"]
	proxy = http://127.0.0.1:1080
[https "https://github.com"]
	proxy = https://127.0.0.1:1080

18.git remote: HTTP Basic: Access denied

使用http方法使用git

λ git push -f origin gj
fatal: User cancelled the authentication prompt.
Username for 'http://gitlab.in.za': za-hongqi
Password for 'http://za-hongqi@gitlab.in.za':
remote: HTTP Basic: Access denied
fatal: Authentication failed for 'http://gitlab.in.za/hongqi/zaip-xman-avatar-product.git/'

远程服务端的用户名和密码与当前系统中git保存的用户名和密码有冲突

解决方法:

1. 如果账号密码有变动 用这个命令 git config –-system –-unset credential.helper 重新输入账号密码 应该就能解决了 
2. 如果用了第一个命令 还不能解决问题那么 用这个命令: 
git config --global http.emptyAuth true
3. 如果要保存账号信息 
git config --global credential.helper store

19.清除git缓存

本来有部分文件上传到git的,后面修改了.gitignore,排除了部分文件

所以必须先删除暂存区或者git分支上的文件,同时工作区也将删除

git rm -r --cached .
git add .
git commit -am 'git cache cleared'
git push

上述是建所有文件缓存中的删除,再新增

也可以只删除指定文件,就不需要再次新增了

git rm --cached conf/application.conf

参考:

10 个迅速提升你 Git 水平的提示

git技巧:删除在本地有但在远程库中已经不存在的分支

关于 Git 你所不知道的一些事

Git查看、删除、重命名远程分支和tag

Git远程操作详解

Git 撤销合并

Git 多平台换行符问题(LF or CRLF)

Git push与pull的默认行为

posted @ 2018-08-28 17:08  hongdada  阅读(5878)  评论(1编辑  收藏  举报