On Mon, Mar 04, 2019 at 08:34:39AM -0800, Keshav Bathla wrote: > Currently while working in feature branch i have done some mistake so due > to which i have to come behind one commit (git reset ---hard HEAD^ ) but > the problem i was facing now is that the deleted commit is on remote > feature branch. So now when after changes on local feature branch and try > to push the git thows error that my remote branch is behind one commit. > > Now the solution i have in mind is to force push (git push -f origin > feature branch) but i was strongly discouraged by my project community to > not to do force pushing and i know it's dangerous too. > > So please help me to find alternative solution for that.
The first thing to understand is that force-pushing is *not* a solution for this problem: you explicitly stated that deletion of that commit was an error, so if you were to force-push your current state - which does not have that commit - you would nuke that commit from the remote branch, too. Any real solution should end up with the following state: get that commit into your local branch "as is" and then arrange for the work you have already done on the local branch to be somehow re-applied on top of that commit. It would make your local branch to appear as if the deletion did not happen at all. Now let's do it. First, chances are high you still have the deleted commit available. Let's pretend the branch you're messing with is called "foo", and the remote repository you intend to push it is known as "origin", and the branch you intend to push to is called "foo" in that repository. Now run `git fetch origin` to make sure your local repository has the deleted commit: this command will fetch the state of the "foo" at "origin". Now run `git log` on both "foo" and record (say, copy to the clipboard) the SHA-1 name of the first commit you have created locally on "foo" (compared to "origin/foo"). Let's say that name is deadf00d. Next, run (assuming "foo" is checked out): $ git rebase --onto origin/foo deadf00d~ (notice the '~'). This command tells Git to do the following: 1) Take the commit deadf00d and all its child commits on "foo" and save them away. 2) Reset the head of the currently checked out branch "foo" to point to the same commit "origin/foo" points at. 3) Re-apply the commits saved at the step 1 to the now-current head of "foo". The end result will be that you will have your local work applied on top of how "foo" looks in the remote repo. It will be safe to push then. Be sure to read <https://git-scm.com/book/en/v2/Git-Branching-Rebasing> for more info. -- You received this message because you are subscribed to the Google Groups "Git for human beings" group. To unsubscribe from this group and stop receiving emails from it, send an email to git-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.