Removing merge commits in Git -
here, have our commit log repo. can see "merge branch...into..." commits, , after commit, branch merged.
i need delete merge commit, not discard changes. default rebasing doesn't show merge commits.
if want apply changes of commits without including merge commit (which roland smith pointed out considered bad practice) can use git rebase
following workflow:
- checkout branch want changes on (let's master):
git checkout master
- reset master commit before merge want undo:
git reset --hard <hash of prior commit>
. make sure there no uncommitted changes on branch working on or command wipe them out. - checkout branch containing updates want "merge" master without merging (let's call
dev
):git checkout dev
. - rebase
dev
onto master. puthead
commit of dev upstream recent commit master points to:git rebase master
(see the docs more information). - fix merge conflicts result during rebase (they have happened in normal merge too).
- the rebased branch should have of commits following latest commit on master. port on master, check out master (
git checkout master
) , merge new branchgit merge dev
. pull changes onto master without adding merge commit. - once you're sure updated
master
branch looks okay, you'll have update branch on remote repositories too. because you're undoing prior history, when push branch you'll have use--force
(or-f
) flag remote repo accept changes i.e.:git push origin master --force
.
that's there it. after this, commits on dev
branched off of master should upstream pre-merge commit above.
note: running rebase permanently update commit history changed branches. safety, recommend taking 1 of 2 methods:
- per @dacav's suggestion, keep track of commit hashes each branch before update anything. if things go pear shaped, run
git reset --hard <original_commit_hash>
put branch on original commit. make backup copies of both branches before rebase pointing @ original commit:
git checkout -b master_bk git checkout -b dev_bk
Comments
Post a Comment