Source From Here
Question
I want to push my local files, and have them on a remote repo, without having to deal with merge conflicts. I just want my local version to have priority over the remote one.
How-To
You should be able to force your local revision to the remote repo by using
(e.g. git push -f origin master). Leaving off and will force push all local branches that have set --set-upstream.
Just be warned, if other people are sharing this repository their revision history will conflict with the new one. And if they have any local commits after the point of change they will become invalid.
Update: Thought I would add a side-note. If you are creating changes that others will review, then it's not uncommon to create a branch with those changes and rebase periodically to keep them up-to-date with the main development branch. Just let other developers know this will happen periodically so they'll know what to expect.
Update 2: Because of the increasing number of viewers I'd like to add some additional information on what to do when your upstream does experience a force push. Say I've cloned your repo and have added a few commits like so:
But later the
development branch is hit with a rebase, which will cause me to receive an error like so when I run git pull:
Here I could fix the conflicts and commit, but that would leave me with a really ugly commit history:
It might look enticing to use
git pull --force but be careful because that'll leave you with stranded commits:
So probably the best option is to do a
git pull --rebase. This will require me to resolve any conflicts like before, but for each step instead of committing I'll use git rebase --continue. In the end the commit history will look much better:
Update 3: You can also use the --force-with-lease option as a "safer" force push, as mentioned by Cupcake in his answer:
Question
I want to push my local files, and have them on a remote repo, without having to deal with merge conflicts. I just want my local version to have priority over the remote one.
How-To
You should be able to force your local revision to the remote repo by using
(e.g. git push -f origin master). Leaving off
Just be warned, if other people are sharing this repository their revision history will conflict with the new one. And if they have any local commits after the point of change they will become invalid.
Update: Thought I would add a side-note. If you are creating changes that others will review, then it's not uncommon to create a branch with those changes and rebase periodically to keep them up-to-date with the main development branch. Just let other developers know this will happen periodically so they'll know what to expect.
Update 2: Because of the increasing number of viewers I'd like to add some additional information on what to do when your upstream does experience a force push. Say I've cloned your repo and have added a few commits like so:
- D----E topic
- /
- A----B----C development
Here I could fix the conflicts and commit, but that would leave me with a really ugly commit history:
- C----D----E----F topic
- / /
- A----B--------------C' development
- D----E topic
- development
- D'---E' topic
- /
- A----B----C' development
沒有留言:
張貼留言