Step backward a few changes to git branches

I'm still new to git, so bear with me. I started adding a function to my project in my current branch and completed it, and then found out that I needed to add a more important function. (If I thought about this, I would just put the new function in another branch, but alas - with the hindsight of 20/20.)

I want to go back to the previous commit, add a more important function, and then add a less important function that I have already completed. Any ideas?

Thanks in advance.

+4
source share
3 answers

This means rewriting the commit history of your branch, which is possible (practical) only if you have not clicked the specified branch on another repo yet.

If you have not clicked on this thread, you have:

o---x---x---F1a---F1b---F1c <-- current branch 

Mark it as an F1 branch

 $ git branch F1 o---x---x---F1a---F1b---F1c <-- current branch, F1 

Reset current branch to F1

 $ git reset x o---x---x---F1a---F1b---F1c <-- F1 ^ | current branch 

Make your function 0 F0 (the one that should have been executed before F1)

 $ git commit ... o---x---x---F0a---F0b <-- current branch ^ | ---F1a---F1b---F1c <-- F1 

Expand the F1 branch over the current branch

 $ git checkout F1 $ git rebase current $ git checkout current $ git merge F1 # fast-formward merge o---x---x---F0a---F0b---F1a'---F1b'---F1c' <-- current branch, F1 

If you have already pressed this function, some are returned in order (see Diego's answer )

+4
source

Probably the best way is to check out the new branch based on the point where you want to add the new feature.

 git checkout -b newfeature <oldcommit> 

Where <oldcommit> is either a commit identifier or a relative link, such as (for example) HEAD~3 three commits before the current HEAD .

After completing the function, you can return to the original branch and choose to either merge it or overload the work on the newfeature branch. One of the great things about git is that you don’t need to order all of your work sequentially; indeed, it often makes sense not to force your story into a single linear sequence of commits.

+8
source

If you just went to your local branch, this is a simple task.

  • go to another branch: git checkout -b moreimportantfeature . No changes will be made in the new branch. This way you can add a new function and click on the remote repo or do git rebase otherbranch to push both changes.

If you clicked on the repo:

  • use git revert HEAD (or some variant of HEAD ^ n if you want to go back to more commits). Then click your changes.
0
source

Source: https://habr.com/ru/post/1305671/


All Articles