1. 程式人生 > >git使用情景3:最常見的解決衝突

git使用情景3:最常見的解決衝突

git使用情景3:最常見的解決衝突

2017年02月23日 13:09:58 天空還是那麼藍 閱讀數:9297更多

個人分類: Git

原生代碼修改完畢,該提交了,開始準備提交

【看一眼當前狀態,命令:git status】

B000000095605B:test baidu$ git status

On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
modified:   gradle.properties

no changes added to commit (use "git add" and/or "git commit -a")

【看到了自己只修改了一個檔案,好的,執行add】

B000000095605B:test baidu$ git add .

【執行commit】

B000000095605B:test baidu$ git commit -m "我修復了一個bug"
[master 8e1d5bb] 我修復了一個bug

 1 file changed, 1 insertion(+)

 

【執行push到遠端】

B000000095605B:test baidu$ git push
To https://github.com/yiwowang/test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/yiwowang/test.git'
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.

【我靠,什麼情況?沒有push成功,咋被伺服器拒絕了呢。原來是這個檔案有衝突,也就是遠端倉庫的此檔案被另一個人改了,好吧,我把遠端程式碼拉下來吧】

B000000095605B:test baidu$ git pull --rebase

remote: Counting objects: 3, done.
remote: Total 3 (delta 1), reused 1 (delta 1), pack-reused 2
Unpacking objects: 100% (3/3), done.
From https://github.com/yiwowang/test
   d239d59..f4b4a74  master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: 我修復了一個bug
Using index info to reconstruct a base tree...
M gradle.properties
Falling back to patching base and 3-way merge...
Auto-merging gradle.properties
CONFLICT (content): Merge conflict in gradle.properties
error: Failed to merge in the changes.
Patch failed at 0001 我修復了一個bug
The copy of the patch that failed is found in: .git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

【可以看到這句話:CONFLICT (content): Merge conflict in gradle.properties,說明這個檔案衝突,我就開啟這個檔案解決衝突】

【衝突解決完畢,然後看一下狀態】

B000000095605B:test baidu$ git status
rebase in progress; onto f4b4a74
You are currently rebasing branch 'master' on 'f4b4a74'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)
Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)


both modified:   gradle.properties
 

no changes added to commit (use "git add" and/or "git commit -a")

【執行add】

B000000095605B:test baidu$ git add .

【看一下所處於的分支,發現是一個臨時分支,還沒有rebase完成】


B000000095605B:test baidu$ git branch
* (no branch, rebasing master)
  dev

  master

【繼續rebase】

B000000095605B:test baidu$ git rebase --continue

Applying: 我嘗試提交

【看一下狀態,說明可以push了】

B000000095605B:test baidu$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

【執行push】

B000000095605B:test baidu$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 314 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/yiwowang/test.git

   f4b4a74..6e7cf9a  master -> master

【在看一下狀態,“沒有需要提交的了,工作空間是乾淨的”】

B000000095605B:test baidu$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean