So for this step, the actual flow will look like this: git checkout starting-point-branch # it will bring all commit changes but leave them unstaged Either you bring only the changes from your commits and submit your work as a new commit:.# it will bring all commit changes but also stage them To do this, checkout to the branch where you want to apply changes: git checkout starting-point-branchĪnd there you have 2 options that we'll only impact the history but in essence are doing the same: It also applies to other scenarios as well where you simply want to duplicate some commits from one branch to another. This scenario is somehow similar to the previous one, except this time you already pushed your commits to the remote repo. Scenario 2: Duplicate all my commits in a new commit on a different branch ( git cherry-pick) It's always a good practice to create a backup branch before making any changes to ensure you can easily recover from any mistakes. Please note that git reset -soft is a powerful command that should be used with caution, as it can potentially overwrite or delete commits permanently. This will create a new commit on feature/branch2 with the changes that you transferred from feature/branch1 Now, switch to the target branch feature/branch2 using the following command: git checkout feature/branch2įinally, commit the changes to feature/branch2 using the following command: git add. If HEAD is pointed at sha-3 when we start, and then perform a git reset –soft sha-1, HEAD will move to that commit.Īll of the changes that were committed in sha-2 and sha-3 are preserved and re-added to the index as staged changes. Git reset -soft HEAD~2 #undo last 2 commits but keep the changes unstaged Run git reset -soft followed by the commit hash of the last commit before your changes. Let's say your log looks like this: #the work added by you, that's not yet in sync with the live repo Now, use git log to identify the commit(s) that you want to transfer to feature/branch2. Next, switch back to the original branch feature/branch1 using the following command: git checkout feature/branch1 You can do this by running the following command: git checkout -b feature/backup-branch Let's say you're currently on a feature branch called feature/branch1 and you've made some commits that you want to transfer to another branch called feature/branch2.įirst, create a backup branch to safeguard your changes in case anything goes wrong. In such cases, you may need to switch all your commits to the correct branch. Perhaps you started making changes on a feature branch, only to later realize that you should have been working on a different branch, such as a bugfix branch or a release branch. One common scenario that can make evenings frustrating is when you realize that you've been working on the wrong branch □♂️ In this article, we'll explore some scenarios where you have to switch your commits/changes to a different branch, so you can enjoy your weekend without the dark cloud on the horizon of unresolved git conflicts □ Scenario 1: Switch your unpushed commits to a different branch ( git reset -soft) Git is notorious for being one of those tasks that can be super annoying and frustrating, especially when you're just about to clock out for the weekend. If you've been in this situation before, you're not alone. However, as you try to execute the final git command, you realize you're working on the wrong branch and you're met with unexpected roadblocks that threaten to derail your weekend plans. You're feeling accomplished and ready to wrap up for the weekend, but there's one final step left - pushing your work to the team's Git repository. Picture this: It's Friday evening, and you've been working hard all week to complete a task assigned to you by your team.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |