At the beginning the local branch is uptodate and now I use git rebase -i HEAD~3
and modify the pick
of the two newest commits to fixup
to combine 3 commits at the local branch,and now I want to push the local branch to the remote. And I only want to show a combined commit at the remote.
However,after combined the local branch falls behind with remote by two commits and I cannot push directly. How can I deal with it?
Asked
Active
Viewed 140 times
0

david
- 842
- 2
- 8
- 25
-
https://stackoverflow.com/q/5189560/1456253 – code11 May 23 '18 at 14:51
-
https://stackoverflow.com/a/8940299/1135424 – nbari May 23 '18 at 14:57
-
@nbari there is sth different. At the beginning the local and the remote is the same. So when I combine 3 commits locally, the local and remote diverge. – david May 23 '18 at 15:13
-
Possible duplicate of [Squash my last X commits together using Git](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git) – phd May 23 '18 at 16:55
1 Answers
2
DISCLAIMER: git push --force
is dangerous; use it at your own risk.
If you really want to do this and combine commits, you could do it on your local repository using git squash
and then git push --force
in order to overwrite the content on the remote.
--
Note that git push --force
will update the branch to point to your new commits, so the old commits will be inaccessible permanently unless you have the commit identifier or a way to find it and the commit hasn't been garbage-collected. This is generally not possible on hosted git services, though GitHub does have an activity log.
In addition, git push --force
can cause issues if other people are working on the same branch and are not expecting the change.

Norman Percy
- 858
- 1
- 8
- 13
-
A "forced push" will not overwrite anything. Instead, it uploads the new commits to the remote and forces the remote branch to point to the last of those. The previous commits will still be accessible from the reflog, both on the client and the server. The "dangerous" part of a forced push is when you collaborate with others on the same branch and they do not expect such a change. – Stephan Windmüller May 23 '18 at 20:51
-
Okay, maybe my answer is badly written. I'll update it to be better phrased. However, in many cases, including most hosted git sites, the reflog is inaccessible, which makes force pushes permanent. – Norman Percy May 23 '18 at 20:57