我有两个分支 A 和 B,它们已经分叉了。
我想定期将它们相互同步(将大部分更改从 B 提交到 A,并将大部分更改从 A 提交到 B)。
在任一方向合并时,这可能涉及拒绝整个文件或仅文件的一部分。
我做的第一件事是:
git checkout A
git merge --squash B
我不关心 B 的提交历史,事实上我什至不想要它。
我只想要一个承诺,上面写着“从 B 带来更改”。
我解决了所有的更改并将其提交给A。然后我做了相反的事情:
git checkout B
git merge --squash A
此时 git 只识别出几个文件已更改。具体来说,如果我运行
git diff --name-only A
,我会得到大量差异列表,大约有 100 个不同的文件。
但是
git merge A
只想合并其中大约 5 个文件中的更改,其他文件完全被忽略。
如何让 git 尝试合并两个分支之间不同的any文件?
一切都如预期。
#!/bin/bash
###
### Merge conflict demo
###
# Create a new demo repository
rm -rf /tmp/tempRepository
echo -e ""
echo -e "* Preparation(s)"
echo -e ""
mkdir -p /tmp/tempRepository
git init /tmp/tempRepository
cd /tmp/tempRepository
echo -e "------------------------------------------------------------------------"
echo -e "$ git checkout -b branchA"
echo -e ""
# Create a new branch
git checkout -b branchA
# Add some content to it
echo -e "------------------------------------------------------------------------"
echo -e "* Initial commit to branchA"
echo -e ""
echo "Hello World" > hello.txt
git add hello.txt
git commit -m "Initial commit"
# Create a new branch from branchA
echo -e "-------------------------------------------------------------------------"
echo -e "* Create branch B branchA (and dont switch to branchB)"
echo -e ""
echo -e "$ git checkout -b branchB"
echo -e ""
echo -e "git branch branchB"
git branch branchB
# Add some content to it (and generate conflict)
echo -e "-------------------------------------------------------------------------"
echo -e "* Create conflict (for the demo)"
echo -e ""
echo "Hello World2" > hello.txt
git add hello.txt
git commit -m "Commit on branchA"
# Add some ramdom commits to branchA
echo -e "-------------------------------------------------------------------------"
echo -e "* Add random content to branchA"
echo -e ""
for i in {1..5};
do
echo "$RANDOM" > A-$RANDOM.txt
git add .
git commit -m "Commit [$i] on branchA"
done
# Add some ramdom commits to branchB
echo -e "-------------------------------------------------------------------------"
echo -e "* Checkout & Add random content to branchB"
echo -e ""
git checkout branchB
for i in {1..5};
do
echo "$RANDOM" > B-$RANDOM.txt
git add .
git commit -m "Commit [$i] on branchB"
done
# Checkout branchA
echo -e "-------------------------------------------------------------------------"
echo -e "$ git checkout branchA"
echo -e ""
git checkout branchA
# Merge branchB into branchA with squash
echo -e "-------------------------------------------------------------------------"
echo -e "$ git merge --squash branchB"
echo -e ""
git merge --squash branchB
# See the diffrence
echo -e "-------------------------------------------------------------------------"
echo -e "$ git diff --name-only branchA"
echo -e ""
git diff --name-only branchA
# Wait for user input
echo "Press any key to continue..."
read -n 1
# Commit the changes
echo -e "-------------------------------------------------------------------------"
echo -e ""
echo -e "* Commit the changes (results of the squashing)"
echo -e ""
echo -e "$ git commit -m"Cool commit""
echo -e ""
git commit -m"Cool commit"
# View the list of chanegs files
echo -e "-------------------------------------------------------------------------"
echo -e ""
echo -e "* Verify that all is "fixed""
echo -e ""
echo -e "$ git whatchanged --oneline | cat"
echo -e ""
git whatchanged --oneline | cat
* Preparation(s)
Initialized empty Git repository in /private/tmp/tempRepository/.git/
------------------------------------------------------------------------
$ git checkout -b branchA
------------------------------------------------------------------------
* Initial commit to branchA
[branchA (root-commit) ea4686a] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 hello.txt
-------------------------------------------------------------------------
* Create branch B branchA (and dont switch to branchB)
$ git checkout -b branchB
git branch branchB
-------------------------------------------------------------------------
* Create conflict (for the demo)
[branchA a191a9f] Commit on branchA
1 file changed, 1 insertion(+), 1 deletion(-)
-------------------------------------------------------------------------
* Add random content to branchA
[branchA 9ff9d92] Commit [1] on branchA
1 file changed, 1 insertion(+)
create mode 100644 A-25691.txt
[branchA 4e1895e] Commit [2] on branchA
1 file changed, 1 insertion(+)
create mode 100644 A-4584.txt
[branchA da270d3] Commit [3] on branchA
1 file changed, 1 insertion(+)
create mode 100644 A-1640.txt
[branchA c11603f] Commit [4] on branchA
1 file changed, 1 insertion(+)
create mode 100644 A-9053.txt
[branchA 3bf2944] Commit [5] on branchA
1 file changed, 1 insertion(+)
create mode 100644 A-29522.txt
-------------------------------------------------------------------------
* Checkout & Add random content to branchB
[branchB 8fe06be] Commit [1] on branchB
1 file changed, 1 insertion(+)
create mode 100644 B-14114.txt
[branchB bbf6b27] Commit [2] on branchB
1 file changed, 1 insertion(+)
create mode 100644 B-1005.txt
[branchB 26fc10d] Commit [3] on branchB
1 file changed, 1 insertion(+)
create mode 100644 B-21027.txt
[branchB 6b6b7e3] Commit [4] on branchB
1 file changed, 1 insertion(+)
create mode 100644 B-29109.txt
[branchB 1caec06] Commit [5] on branchB
1 file changed, 1 insertion(+)
create mode 100644 B-28991.txt
-------------------------------------------------------------------------
$ git checkout branchA
-------------------------------------------------------------------------
$ git merge --squash branchB
Squash commit -- not updating HEAD
-------------------------------------------------------------------------
$ git diff --name-only branchA
B-1005.txt
B-14114.txt
B-21027.txt
B-28991.txt
B-29109.txt
Press any key to continue...
-------------------------------------------------------------------------
* Commit the changes (results of the squashing)
$ git commit -mCool commit
[branchA 67fc818] Cool commit
5 files changed, 5 insertions(+)
create mode 100644 B-1005.txt
create mode 100644 B-14114.txt
create mode 100644 B-21027.txt
create mode 100644 B-28991.txt
create mode 100644 B-29109.txt
-------------------------------------------------------------------------
* Verify that all is fixed
$ git whatchanged --oneline | cat
67fc818 Cool commit
:000000 100644 0000000 7929c6f A B-1005.txt
:000000 100644 0000000 a52de05 A B-14114.txt
:000000 100644 0000000 58041e2 A B-21027.txt
:000000 100644 0000000 df4436a A B-28991.txt
:000000 100644 0000000 739174e A B-29109.txt
3bf2944 Commit [5] on branchA
:000000 100644 0000000 fd2a90e A A-29522.txt
c11603f Commit [4] on branchA
:000000 100644 0000000 81b230a A A-9053.txt
da270d3 Commit [3] on branchA
:000000 100644 0000000 f230891 A A-1640.txt
4e1895e Commit [2] on branchA
:000000 100644 0000000 90850f4 A A-4584.txt
9ff9d92 Commit [1] on branchA
:000000 100644 0000000 15de1c7 A A-25691.txt
a191a9f Commit on branchA
:100644 100644 557db03 b2b6f00 M hello.txt
ea4686a Initial commit
:000000 100644 0000000 557db03 A hello.txt