Git 解决冲突

  • 创建远程仓库:git init --bare
yuzhenteng@yuzhentengdeMac-mini xy.remote % git init --bare
Initialized empty Git repository in /Users/yuzhenteng/Desktop/demo/remote_repo/Lxy.remote/
  • 创建本地仓库:git init
yuzhenteng@yuzhentengdeMac-mini xy % git init
Initialized empty Git repository in /Users/yuzhenteng/Desktop/demo/local_repo/lxy/.git/
  • 添加远程仓库
yuzhenteng@yuzhentengdeMac-mini xy % git remote add origin /Users/yuzhenteng/Desktop/demo/remote_repo/xy.remote
yuzhenteng@yuzhentengdeMac-mini xy % git remote
origin
yuzhenteng@yuzhentengdeMac-mini lxy % git push          
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin main

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

以上报错是因为本地分支和远程分支没有关联,关联如下:

yuzhenteng@yuzhentengdeMac-mini lxy % git branch -t --set-upstream-to=origin/main main
branch 'main' set up to track 'origin/main'.
yuzhenteng@yuzhentengdeMac-mini next % 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=<remote>/<branch> main

这个也是报没有和远程分支关联

xy和next都修改file.txt xy先更改且上传远程仓库origin main 然后next修改file.txt 保存并提交 当push的时候提示如下:

yuzhenteng@yuzhentengdeMac-mini next % git pull  
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.

我选择git config pull.rebase false # merge这个配置 然后产生冲突:

yuzhenteng@yuzhentengdeMac-mini next % git pull
Auto-merging xy/file.txt
CONFLICT (content): Merge conflict in xy/file.txt
Automatic merge failed; fix conflicts and then commit the result.

解决冲突并push代码之后查看log:

yuzhenteng@yuzhentengdeMac-mini next % git log --oneline --graph --stat
*   9017add (HEAD -> main, origin/main) 解决冲突
|\  
| * 7840788 add shijiazhuang
| |  xy/file.txt | 1 +
| |  1 file changed, 1 insertion(+)
| * f6d7d8e add hebie
| |  xy/file.txt | 1 +
| |  1 file changed, 1 insertion(+)
* | 4e66c82 add hebei
|/  
|    xy/file.txt | 2 ++
|    1 file changed, 2 insertions(+)
* 607e926 add hellow chain
|  xy/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* ac8584a (next/main) xy init
   xy/file.txt | 1 +
   1 file changed, 1 insertion(+)

这种解决冲突的方法很不好,产生了新分支而且多出一个merge提交,解决如下:
先回退代码:

//找到ORIG_HEAD 上一个提交id
yuzhenteng@yuzhentengdeMac-mini next % git rev-parse ORIG_HEAD
4e66c820daebcfcc8f7f82b4dea0693b371db4c7
//回退到add hebei 也就是没有pull之前的状态
yuzhenteng@yuzhentengdeMac-mini next % git reset --hard ORIG_HEAD
HEAD is now at 4e66c82 add hebei

yuzhenteng@yuzhentengdeMac-mini next % git log --oneline --graph --stat
* 4e66c82 (HEAD -> main) add hebei
|  xy/file.txt | 2 ++
|  1 file changed, 2 insertions(+)
* 607e926 add hellow chain
|  xy/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* ac8584a (next/main) xy init
   xy/file.txt | 1 +
   1 file changed, 1 insertion(+)

然后用 git pull --rebase

yuzhenteng@yuzhentengdeMac-mini next % git pull --rebase
Auto-merging files/file.txt
CONFLICT (content): Merge conflict in files/file.txt
error: could not apply 8164f48... 添加河北
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 8164f48... 添加河北

继续解决冲突 然后继续rebase:

yuzhenteng@yuzhentengdeMac-mini next % git rebase --continue
files/file.txt: needs merge
You must edit all merge conflicts and then
mark them as resolved using git add
yuzhenteng@yuzhentengdeMac-mini next % git add .
yuzhenteng@yuzhentengdeMac-mini next % git status
interactive rebase in progress; onto 1ad2042
Last command done (1 command done):
   pick 8164f48 添加河北
No commands remaining.
You are currently rebasing branch 'main' on '1ad2042'.
  (all conflicts fixed: run "git rebase --continue")

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   files/file.txt

yuzhenteng@yuzhentengdeMac-mini next % git rebase --continue 
[detached HEAD ad3ee72] 添加河北
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/main.
yuzhenteng@yuzhentengdeMac-mini next % git log --oneline --graph --stat
* ad3ee72 (HEAD -> main) 添加河北
|  files/file.txt | 1 +
|  1 file changed, 1 insertion(+)
* 1ad2042 (origin/main) 添加河北和石家庄
|  files/file.txt | 2 ++
|  1 file changed, 2 insertions(+)
* dcdeb8b files init
   files/file.txt | 1 +
   1 file changed, 1 insertion(+)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。