0

I have a local branch with commits like: head/master 5 4 3 2 1

I want to move 3 to the head and not lose any other commits: head/master 3 5 4 2 1 Is this what rebase is for? (Obviously, I'm new)

Ideas? thanks in advance

Yogi Bear
  • 943
  • 2
  • 16
  • 32

2 Answers2

1

Use git rebase:

 git rebase -i HEAD~5

After HEAD~ there is number five - it is the same as the commits you have. After executing this command, your editor* should run. It has a list of your last 5 commits (remember HEAD~5?). Now change the position of commit(s) on the list of commits you are presented and save and close the editor. You are done. The position of commits have changed.

* How do I make Git use the editor of my choice for commits?

menteith
  • 596
  • 14
  • 51
  • Nice.. easy to use. Thanks... does the head number have to be the total number of commits in the branch or just large enough to cover the area I want to edit? :-) Much appreciated. – Yogi Bear Oct 30 '19 at 13:38
  • Specify the number of commits you would like to play with, ie. reword, delete, change position. In other words, don't use the number of all the commits (unless you want to deal with all of them of course). – menteith Oct 30 '19 at 14:16
0

If what you want to do is to reorder the revisions, it's simple enough:

git checkout 2
git cherry-pick 3..5 # this will discard revision 3 so changes from revisions 4 and 5 will be applied
git cherry-pick 3 # apply change from revision 3
# if you like the result
git branch -f master
git checkout master
# push -f if needed because you rewrote history
eftshift0
  • 26,375
  • 3
  • 36
  • 60