I have feature
branch and develop
branch and I created feature
from develop
and made changes and created pull request but, now I have some other changes need to be merged from develop as there are some other guys made changes.
So, Steps I did:
git checkout develop
git pull
git checkout feature
git merge develop (after solving some conflicts)
git push
On next day I did same above steps again as there were more commits merged to develop to keep updated my branch.
So, now I want to revert/reset my feature branch without those merged from develop changes but, I couldnt find the way.
I did git log
and git reflog
to check the commits so, I can do
git reset --hard HEAD~>number>
But, now I can see there are lot of commit hash as there are many commit merged to develop branch.
How can I revert/rest those git merge develop
So, I can feature without develop merged and I cab do later on.
Any help would be greatly appreciated
If you want to keep your branch up to date with the latest develop
, you can regularly merge develop
into your branch like you started doing (but apparently regretted), or you can regularly rebase your branch onto the latest develop
. Both strategies produce the same ending state, but rebase will end up with a cleaner history without multiple extra merge commits. Which way is best is a source of great debate, but, generally, if your feature branch has not been pushed yet, or if it has but it's understood that feature branches in your repo can be rewritten, then it seems you will probably prefer rebasing. The fact you are willing to reset your branch, suggests you are fine with rewriting your branch and that you will generally prefer rebasing overall.
Fortunately you can also use rebasing to fix your current situation. Note in the following commands I'll use origin/develop
instead of develop
so that you can skip the part about keeping your local copy of develop
up to date. If you start using the remote tracking develop
branch instead, you can simply delete your local copy of develop
as you won't need it again. (And if you call your remote something other than origin
, change these commands accordingly.)
To rebase your branch onto the latest develop
# git switch feature
git fetch # update all remote tracking branches, including origin/develop
git rebase origin/develop
Note this assumes you already have your feature branch checked out. If not you could either do that first, or you can save typing a few keystrokes by adding your branch to the rebase
command: git rebase origin/develop feature
The first time you do this it should get rid of all of those merge commits, but you will likely have to resolve your merge conflicts again. If you have many commits that are conflicting, another thing you can do is to considering squashing one or more of your commits into fewer first, and then doing the rebase. (Note it's common to use rebase -i
to squash commits.) If you're willing to squash all of your changes into a single commit it's even easier, since you can reset --mixed
back to before you started, and then make a new big commit which will include all of your changes plus all of the changes on develop
that you merged in. After that, rebasing your changes onto the latest origin/develop
should leave you with a new (smaller) commit with just your own changes, because the changes in that (big) commit from develop
will fall out.
After you've finished your rebase you can just repeat the fetch
and rebase
commands anytime you wish to update your branch again with the latest. (As an example I typically rebase my branch at least once or twice per day to keep it up to date, sometimes more if I'm working on files that are modified often by other people.)