0

Cn - parent commit.

Cn+1(green) - local commit.

Cn+1(yellow) - another commit in same branch but already pushed to remote, but have same parent as local commit.

Result: I need to save all changes in local commit(green), but don't override changes in remote commits(yellow). So it look like save changes in local and remote commit relatively to parent, but green with high priority.

I can't do this with merge because it require manual stuff in interactive mode, I need to do it automatically from bash.

enter image description here

alani
  • 12,573
  • 2
  • 13
  • 23
Nitro Boy
  • 53
  • 5
  • 1
    Sounds like you want a rebase, not a merge. – Charles Duffy Jun 15 '20 at 03:37
  • Note that you can get the history of a rebase with the tree state of a merge with judicious soft reset use. – Charles Duffy Jun 15 '20 at 03:37
  • That said, to get a solid answer, we'd need a solid question -- a script that reproduces the tree state you care about when run, for example. – Charles Duffy Jun 15 '20 at 03:38
  • Use `git cherry-pick` to apply commits one by one in order. `git rebase -i` can also do the job. – ElpieKay Jun 15 '20 at 03:39
  • sound like i need to create some copy of parent commit and apply changes to it from remote commits then local changes and rebase HEAD to this commit also add two parents commits as local and last remote – Nitro Boy Jun 15 '20 at 03:39

2 Answers2

0

Assuming that you have a branch, let's say it's called mybranch, which points to C(n+1), and there is another branch some_branch which points to C(n+3), then to produce the structure shown in your image, you can run

git checkout some_branch
git merge mybranch

Another alternative is to transplant the green C(n+1) on top of C(n+3). You can do that by running

git checkout some_branch
git cherry-pick some_branch..mybranch

After either of these, you can run commands like git log --graph to examine the commit history, and then if you're satisfied with it, git push to upload the changes to the remote server.

However, note that in general, neither of these is guaranteed to succeed without interaction. Depending on the changes that are made in the various commits, there may be conflicts which Git cannot resolve on its own. That's a possibility regardless of how you choose to combine the commits.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • I already have branch, but some commits of it are localy, and some are remote, that were pushed before local commit. – Nitro Boy Jun 15 '20 at 04:05
0

seems it does the work

P.S i don't use it to merge code, only values in yaml format

git fetch origin test
PARENT_COMMIT=$(git show --format='format:%H' --no-patch HEAD~1)
LAST_REMOTE_COMMIT=$(git log --all -n 1 --pretty='format:%H' HEAD..origin/test)
git diff --unified=0 $PARENT_COMMIT $LAST_REMOTE_COMMIT | git apply
git add file
git commit --amend --no-edit
LAST_LOCAL_COMMIT=$(git show --format='format:%H' --no-patch HEAD)
git checkout origin/test
git rebase $LAST_LOCAL_COMMIT --strategy recursive -X ours
git push origin test
Nitro Boy
  • 53
  • 5