git merge,push,pull confusion

时间:2015-02-11 16:01:29

标签: git

我有一个git存储库R,它被克隆在机器A和B上。我对A和B上的代码进行了更改。然后我将A推回到R.现在我想合并更改B,让A和B重新同步。

在B上,我跑了git pull。这样做了一些东西。 (我不知道。我怎么检查或者我应该说:git pull --no-commit?)

如果我现在运行,git push,是否会将这些更改放入存储库? 如果是这样,我如何让A重新同步。这是A的git拉?

感谢。

1 个答案:

答案 0 :(得分:2)

在类Unix操作系统上,以下命令将设置您在问题中描述的情况。

mkdir play && cd play
mkdir Rtemp && cd Rtemp
git init
echo "This is file 1" > file1.txt
echo "This is file 2" > file2.txt
git add file?.txt && git commit -m "Initial Commit"
cd ..
git clone --bare Rtemp R
git clone R A
git clone R B
cd A
echo "edited file 1 in A" >> file1.txt
git commit -a -m "Edited file 1 in A"
cd ../B
echo "edited file 1 in B" >> file1.txt
echo "edited file 2 in B" >> file2.txt
git commit -a -m "Edited both files in B"
cd ..

此时,您在R中有一个中央仓库,在AB中有两个克隆。已在file.txtA中修改了B,并且已在file2.txt中修改了B

现在,您将更改从A推升到R

$ cd A
$ git push origin master
Counting objects: 3, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 304 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To .../play/R
   34307cc..2a925ac  master -> master

此时,RA同步。 B有本地更改,并且是R后面的一个提交 - 但在您执行git fetchgit pull中隐含的)之前不知道它。

$ cd ../B
$ git fetch
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From .../play/R
   34307cc..2a925ac  master     -> origin/master
$ 

fetch的输出显示git已下载上游存储库的状态。 git status将向您展示您的表现。

$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)

此处的警告标志是Your branch and 'origin/master' have diverged的注释。这表示你需要进行合并,我在那里设置它的方式应该是冲突

$ git merge origin/master
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:   file1.txt

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

请注意,它只是抱怨file1.txt。 Git能够将更改合并到file2.txt,因为它只在本地更改。如果您现在cat file1.txt,您将看到以下内容

$ cat file1.txt
This is file 1
<<<<<<< HEAD
edited file 1 in B
=======
edited file 1 in A
>>>>>>> origin/master

成对的<<<<<<<>>>>>>显示冲突的区域。真实合并中的每个文件中可能有许多。现在,你需要决定合并后你想要file1.txt的样子,然后编辑它就可以了。假设我们想保留两行,我们的本地编辑发生在上游之后。所以我们编辑它,file1.txt现在看起来像这样:

$ vi file1.txt
$ cat file1.txt
This is file 1
edited file 1 in A
edited file 1 in B

(注意我们删除了冲突标记)

现在我们需要完成合并,并将我们的提交推回到R

$ git add file1.txt 
$ git commit -m "Resolved conflicts"
[master 5454980] Resolved conflicts
$ git log --oneline
5454980 Resolved conflicts
1673d76 Edited both files in B
2a925ac Edited file 1 in A
34307cc Initial Commit
$ git push origin master
Counting objects: 7, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 650 bytes | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To .../play/R
   2a925ac..5454980  master -> master

此时,BR已同步,但现在A再次落后于主人。

$ cd ../A
$ git fetch
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 7 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
From /home/keith/play/gitplay/R
   2a925ac..5454980  master     -> origin/master
$ git status
On branch master
Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

这次git status让我们知道我们可以'快进' - 这意味着不会有冲突。

$ git merge origin/master
Updating 2a925ac..5454980
Fast-forward
 file1.txt | 1 +
 file2.txt | 1 +
 2 files changed, 2 insertions(+)
 git merge origin/master
$ git log --oneline
5454980 Resolved conflicts
1673d76 Edited both files in B
2a925ac Edited file 1 in A
34307cc Initial Commit

最后,ABR都是一致的,开发人员都可以继续工作。

这似乎很多工作,但它是一个非常基本的工作流程,你很快就会习惯。

一旦你真正知道自己在做什么,你就可以使用git来安全地使用git pull及其邪恶(但非常有用)的形式git pull --rebase。但在此之前,请坚持使用git fetchgit merge