Student4K Student4K - 2 months ago 11
Git Question

pull after rebase results into merging conflicts

I have a branch B, that was checked out in another repo, then some changes were done there and after rebase they were push'ed with --force (it was a fixup to the last commit).
Now, if I pull the changed branch into the first repo, I get merging conflicts.

The commands I run (with irrelevant changes, that simplify the actual output) are as follows:

# First checkout the original B
$ pwd
/home/user/dir1

$ git checkout B
Switched to branch 'B'
Your branch is up-to-date with 'origin/B'.

# Now clone the same repo into another directory and checkout B there
$ cd .. && git clone git clone ssh://git@myhost/myrepo.git dir2 && cd dir2

$ git checkout B
Switched to branch 'B'
Your branch is up-to-date with 'origin/B'.

# Now edit some file, commit it and "merge" the changes
# to the originally last commit as a fixup
$ nano file.txt
$ git commit -a -m "Fixup to file.txt"
$ git rebase -i HEAD~2
... the last commit is "fixup'ed" to the last commit in the original branch (the commit before "Fixup to the file.txt" commit

# Push the B with rewritten history to the upstream
$ git push --force origin B

# Get back to the original repo and pull the changes
$ cd ../dir1
$ git pull origin B
...
CONFLICT (content): Merge conflict in .../file.txt
...


The only difference between the two copies of the branch is in the "top" commit.
Is it possible to tell git, that I want to replace the last commit in the first repo with what I have in the upstream without git-pull trying to look inside for changes in the corresponding files?

Answer

With a history like

...-A-B-C-D-E     # HEAD

Where E is the unchanged commit in dir/. We will introduce E' as a commit that originated from E but was rebased and force-pushed from dir2.

In dir you can rewind your HEAD to be on the prior commit:

git reset --hard HEAD^

to get

...-A-B-C-D        # HEAD
           \
            `-E    # dangling and invisible, will be garbage collected

afterwards

git pull upstream B

will give you whatever was there

...-A-B-C-D-E'     # HEAD
           \
            `-E    # dangling and invisible, will be garbage collected

Be warned though, you will lose the content of E (unless you search for it in reflog)

Comments