0

Can someone explain what could happend.

I pushed first commit to server. Was ok. I created second commit. In other bash window I started git add -p, but that window froze, so I moved to other window and continued git add -p, git commit --amend, git push and wierd thing happend. Btw. it was first push (no -f was needed)

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

I did git push,

$ git push
 ! [rejected]  feature-1 -> feature-1 (non-fast-forward)
error: failed to push some refs to ...

then git pull

$ git pull
remote: Counting objects: 136, done.
remote: Compressing objects: 100% (136/136), done.
remote: Total 136 (delta 65), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (136/136), 33.88 KiB | 0 bytes/s, done.
Resolving deltas: 100% (65/65), done.
From github.com:.../...
   444444..444444  other-feature-1 -> origin/other-feature-1
   222222..222222  other-feature-2 -> origin/other-feature-2
 + 333333...333333 other-feature-3 -> origin/other-feature-3  (forced update)
Merge made by the 'recursive' strategy.

then

$ git status
On branch feature-1
Your branch is ahead of 'origin/feature-1' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

Why this happend?

On server are all changes I need(both commits are correct). No idea why this poped up: Your branch is ahead of 'origin/feature-1' by 2 commits.

Note about forced update (of other-feature-3): the owner of the branch did force push. What I worry about is if I did not break anything. And how can I keep on working on this branch?

I was thinking about removing local feature-1 branch and checkout again from the server (origin/feature-1) branch.

Thanks.

zombie_ghast
  • 1,713
  • 15
  • 21
  • If you don't care about your local changes you could just do a hard reset back to the commit you want from the server. For example, `git reset --hard 0d1d7fc32` would reset you back to `0d1d7fc32` on the server, getting rid of any new local commits you have made since then. – Mogzol Oct 19 '16 at 15:16
  • 1
    Doing a reset back to the server's `origin/feature-1` should get rid of your 2 mystery commits and fix that message. Use the command `git reset --hard origin/feature-1` – Mogzol Oct 19 '16 at 15:22
  • Using `git commit --amend` "rewrites history": http://stackoverflow.com/a/25948372/1256452 (this happens even if you are not in "detached HEAD" mode). This is what caused the non-fast-forward rejection error. – torek Oct 19 '16 at 15:26
  • mhm yea, just realized that git locally added merge commit and doubled second commit. Looks like removing last two commits solves the problem. – zombie_ghast Oct 19 '16 at 15:26

1 Answers1

0
  • This could happend only if

    • someone else pushed commit to remote

      • you could have done rebase origin to keep linear history (without merge)

      • that's why it's recommended to pull before commit

    • you have pushed to remote before doing amend

      • When doing amend git destroys previous commit and create new one with new calculated Sha1 hash. That's where diverge comes from
  • when you pulled then git recursively merged remote branch with yours, that's second commit (with amended one) ahead of origin

Zildyan
  • 1,261
  • 9
  • 11
  • No one pushed and I didn't push first before amending (first amended then pushed). That's why this is wierd. – zombie_ghast Oct 19 '16 at 15:33
  • Ok but maybe you amended previous commit, there arised divergence between sha1 hashes and I believe somehow you must did that, or someone else rewrote history – Zildyan Oct 19 '16 at 15:38
  • I didn't have to use -f at all. I didn't realize on the beginning Git doubled second commit and added merge commit. So I removed both(doubled and merge commit). It fixed the problem. But still am wondering how this happened. – zombie_ghast Oct 19 '16 at 15:41
  • 1
    As I commented earlier, and Zildyan put into his answer, using `git commit --amend` *copies* the original commit to a new copy. Your `git pull`, which runs `git merge`, then merged your new copy with your original commit, still reachable on the remote-tracking branch. – torek Oct 19 '16 at 16:22