WANG LH , Research & Development

git不常用命令

2017.09.07 19:38

git提交解决冲突

git命令在提交代码前,没有pull拉最新的代码,因此再次提交出现了冲突。

error: You have not concluded your merge (MERGE_HEAD exists).
hint: Please, commit your changes before merging.
fatal: Exiting because of unfinished merge.

解决方法有两种

  1. 保留你本地的修改

git merge --abort
git reset --merge
合并后记得一定要提交这个本地的合并(add-->commit-->push-->pull)
然后在获取线上仓库
git pull

  1. down下线上代码版本,抛弃本地的修改

不建议这样做,但是如果你本地修改不大,或者自己有一份备份留存,可以直接用线上最新版本覆盖到本地
git fetch --all
git reset --hard origin/master
git fetch

从git远程仓库中pull最新的代码,出现如下错误:Please commit your changes or stash them before you merge.

解决方法如下:(git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug, 先stash, 使返回到自己上一个commit, 改完bug之后再stash pop, 继续原来的工作。)
1: git stash //暂存代码
2: git pull 分支名//从远程仓库拉取最新代码
3: git stash pop //合并代码到本地仓库 此时代码是将暂存的代码和远程仓库的代码合并,如下图:


4:这时候需要手动修改合并所需的代码即可。
5:git stash clear//需要清空git栈执行该命令
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。
git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了

git push 报错,如下:


解决命令:git pull --rebase origin 你的分支名称,如下图所示


再次执行push命令:如下图所示:

git push 还会报下面的错(如图所示):这多是多人开发有了冲突。

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


解决命令如下:
git push -u 代码所在的分支 -f //强制提交,此时远程上的修改已经被覆盖。这种方法一般不建议使用,除非你把远程上修改的代码复制到本地。

本地回退历史版本,当提交代码发生冲突或者想回退到某一个版本,操作如下

1:git log (获取提交的历史日志)


2: 执行命令:git reset --hard 版本号(就是git log 中的 commit后面的哈希值(上图中的黄色部分 commit 后面的值))
3:想要修改远程上的代码还需要执行如下命令:
git push -u 代码所在的分支 -f //强制提交,此时远程上的修改已经被覆盖。这种方法一般不建议使用,除非你把远程上修改的代码复制到本地。

git flow publish release

  1. git checkout master
  2. git pull
  3. git chekcout develop
  4. git pull
  5. git flow release finish v1.5.2
  6. git push
  7. git push --tag

Git配置别名

  1. vi ~/.gitconfig
[alias]
    pull = pull
    co = checkout
    ci = commit
    st = status
    df = diff
    pl = pull -p
    ps = push
    br = branch
    mg = merge
    ds = describe
    tree = log --graph --all --remotes=origin
    fl = flow
    fh = flow help
    feature = flow feature
    fe = flow feature
    release = flow release
    re = flow release
    hotfix = flow hotfix
    hf = flow hotfix

[user]
    name = wanglianhai
    email = wanglh7@163.com
[alias]
    pull = pull
    co = checkout
    ci = commit
    st = status
    df = diff
    pl = pull -p
    ps = push
    br = branch
    mg = merge
    ds = describe
    tree = log --graph --all --remotes=origin
    fl = flow
    fh = flow help
    feature = flow feature
    fe = flow feature
    release = flow release
    re = flow release
    hotfix = flow hotfix
    hf = flow hotfix
[core]
        autocrlf = input