2

I cloned a repository to my local machine.Then, i created a new branch (from the current master )using git checkout -b dev/abc/test1. I am the only person working on this branch. I worked on this branch for a few hours and then made commits and did git pushand created a pull request. Reviewers made some comments on this pull request.

Before starting to work on those comments, i did, git checkout master, then, git pull to update master branch. Later, i wanted to merge those changes(there were a lot many changes in master since i had last updated it) to my branch dev/abc/test1.

Hence, i did git checkout dev/abc/test1. Then i did git reset --hard origin/master and then git merge origin/master. So, all my changes from this branch were lost as expected. I manually copy pasted the changes i made from the pull request. Now i started to work on those reviewer comments and fixed all of them, did a git commit and then git push but only to see the message

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart.

To avoid this, i went ahead with git push --force.

Will this have any negative impact? I see one of the comments saying here Cannot push to GitHub - keeps saying need merge that 'forcing' is most of the time, if not always, the wrong answer. Will my approach impact the repository in any negative way ? (As a side note, i am the only individual contributor to the branch dev/abc/test1)

phd
  • 82,685
  • 13
  • 120
  • 165
fsociety
  • 977
  • 3
  • 12
  • 23

3 Answers3

3

If you are in a GitHub pull request review process, then force pushing is acceptable. Doing so will update your pull request automatically and GitHub will detect what has changed.

Another common workflow for the review process is to just add commits that address the feedback. So you can just push those directly (without force). At the end, it is then usually up to the person responsible for merging to squash those commits so that the individual fixes that went on top of your initial PR will not appear separately.

Force pushing is generally just frowned upon because it often means that you are rewriting history (something that rebasing does). And publishing rewritten history is a very good way to mess with all your team members, so you should really avoid that at all costs. A pull request is however always considered to be in a fluid state (often requiring rebasing to match ongoing development on the base branch), so people are less likely to expect that to have a consistent history.

poke
  • 369,085
  • 72
  • 557
  • 602
  • How does github show the reviewers what changed? I started to review a PR (I read some of the code in the Files Changed panel and clicked the Viewed checkbox on some of the files), the author force-pushed, and now I only see the new code and I see no way to know what changed. – gelisam Feb 03 '22 at 14:29
  • I figured it out: turns out the word "force-pushed" in the Conversation panel is a hyperlink to a diff between before and after the force-push. – gelisam Feb 03 '22 at 14:32
1

When you did git push --force, you forcibly overwrote the remote dev/abc/test1 branch. This means that the previous remote branch was potentially erased for good. There should be no side effects in your case, because you are the only one using the branch. The reason why force pushing is generally bad, is because in general remote branches are shared by several developers at a time. When you rewrite the history of a remote branch, you can cause havoc for anyone else sharing the branch. When that other person goes to git pull, they will see a new history coming in.

Note that, perhaps ironically, the best thing for you to have done would be to just replace your local copy of dev/abc/test1 with the remote branch. So, instead of doing a hard reset to master, you could have done a hard reset to the dev/abc/test1 tracking branch, via:

git reset --hard origin/dev/abc/test1

This would have undone whatever you did locally which might have prompted you to reset to master.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

In this case you are rewriting the history of your dev/abc/test1 branch. As long as nobody else is using this branch then it should be OK.

I would suggest the following workflow, that will mean you don't need to force push next time.

git checkout --branch dev/abc/test1
git checkout master
git pull
git checkout dev/abc/test1
git merge master
## Resolve merge conflicts
git commit
git push

This would have the same result while not rewriting history.

Jim Wright
  • 5,905
  • 1
  • 15
  • 34
  • There is no advantage to use a merge workflow for someone who is working alone on a feature branch. force push on a feature branch is a valid rebase workflow in this case. – Étienne Feb 14 '20 at 12:14
  • Yes I agree, as I stated in my answer "As long as nobody else is using this branch then it should be OK." – Jim Wright Feb 14 '20 at 12:35