git - Safe way to 'reshape' a branch's history -


often, when using git, make mistake of rebasing or merging didn't want. example, have 3 commits don't want show in history, , 1 do, a-b-c-d, d commit want. run

git rebase -i head~4 

and squash a, b, , c. then, later, decide wanted have 2 commits, because a totally unrelated other 3 , history make more sense if on own. is,

git reflog | grep 'rebase -i'  

which spit out list ends

<somehash> head@{<num>}: rebase -i (start): checkout head~4 

and run

git checkout head@{<num>+1} git br -d <branchiwasworkingon> git checkout -b <branchiwasworkingon> git rebase -i head~4 

and same thing did before, picking commit a time. cumbersome, , requires deleting branches, makes me nervous workflow isn't particularly safe. question is, there more secure way rewrite history reflog & branch deletion method?

my favorite way split commits if necessary git reset --soft head~1 bring changes of last commit, in case 4 merged commits, staging area. there can recommit them see fit. basic flow having commits such a-b-c-dwhere d multiple commits you'd break up. run git reset --soft head~1 , you'll have a-b-c d's changes in staging area. commit many separate times you'd two, , you'd have a-b-c-e-f.

now thats great if commit @ top of head. if need go several commits, you'll still need create branch, git reset --hard head~n remove commits (don't worry they're safe on other branch). run git reset --soft head~1. once you've recommitted whatever you'd like, can rebase original branch off branch you'd be.

i can't cleaner method, might bit easier. me @ least easier visualize whats happening. being said, if you're rewriting history, going bit messy. sure take time , mindful of whats getting deleted when.

also, nice side note, turns out rebasing commits out of branch doesn't make them go away. git caches everything. i'd deleted commit shouldn't have before, because knew commit hash scrolling in terminal, able cherry-pick existence. not lost when delete something.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -