If you follow me you know I usually go with git aliases so in the video you’ll see me using git lgo instead of git log -oneline. Let’s see how we can use it in our scenario.įirst of all, you need to get the commit hash of the commit you want to move. What cherry-pick does is basically copy a commit from one branch to another (by generating a new commit, with the same changes). In this case the plan is not to remove the commit than commit it back, but to just move it to the other branch (and then you can cleanup the old branch). Move to an existing branch (cherry-pick)Ī similar way to move commits between branches is to use git cherry-pick. With git stash pop you basically remove the top element from the stack and apply it.Īt this point you might have some conflicts (that’s why we used stash instead of just doing checkout with the uncommitted changes) but it’s not a problem if you watched my video on how to resolve merge conflicts. With the first git stash you added a new element on top of the stack. Why pop? Because the stash is actually a stack. It’s now time to switch to the right branch: git checkout feature/new-cool-stuffĪnd finally, you can reapply your changes with: git stash pop Fear not, if you run git stash list you’ll see that they’re still there. Now you can stash them: git stashĪs you can see now, all your changes have disappeared. Since we used -soft the changes are still there, they’re just not committed. You can do it with a soft reset: git reset -soft HEAD~1 Let’s see how.įirst of all, you need to get rid of the commit. The plan here is to get rid of the commit, put the changes somewhere (stash) and then reapply the changes in the right branch so you can commit again. This is where stashing comes in handy.īut we were talking about a commit in the wrong branch and you cannot stash a commit (or can you? Not that I know). If your changes are conflicting with the new branch you’re trying to switch to, you’ll get an error. If you have uncommitted changes you can still switch from a branch to another, but not always. You can do it by stashing the commit and then popping it on the other branch. Let’s shift the focus a little bit, you can be on any branch and you want to move the last commit to another branch. What happend here? You removed from main the commit you just did, but you didn’t lose it. Step #2 is as easy as an hard reset to the previous commit: git reset HEAD~1 You can do a quick check with: git log -oneline We want to stay on main for now.Īt the current state, you have both main and feature/new-cool-stuff pointing to the same commit and you’re still positioned on main. Note: I intentionally did not use git checkout -b feature/new-cool-stuff because that command also moves you to the new branch. This means that step #1 is: git branch feature/new-cool-stuff So, if you’re on main and you create a new branch, you’ll start from the current state of main (which is the commit you just did). Think about it, when you create a new branch you basically start from the current situation of the branch you’re on. You did a commit on main but you actually wanted to move to a new branch. This is the easiest scenario and is exactly what is described above.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |