乐闻世界logo
搜索文章和话题

How to moving existing uncommitted work to a new branch in git?

3个答案

1
2
3

When working with Git, you might find yourself making changes on the wrong branch or decide that your modifications should be on a new branch to keep the main branch clean or for other reasons. Fortunately, Git offers flexibility to move uncommitted changes to a new branch. Here are the steps:

  1. Check current changes: Before moving the changes, check the status of your working directory and staging area. You can use the following command:

    shell
    git status

    This will display the status of your current changes, whether they are staged or unstaged.

  2. Create and switch to a new branch: If you have staged the changes, unstage them first (if you intend to move them to the new branch). Then, create and switch to a new branch using the following command:

    shell
    git checkout -b new-branch-name

    This command creates a new branch named new-branch-name and switches to it.

  3. Add and commit changes: Now that you are on the new branch, add and commit your changes. Use the following command to stage all changes:

    shell
    git add .

    Or, if you want to add specific files, use:

    shell
    git add <file-path>

    Next, commit the changes to your new branch:

    shell
    git commit -m "Commit message explaining the changes"
  4. (Optional) Keep the main branch clean: If you just created the new branch from the main branch (e.g., main or master) and don't want these changes to appear on the main branch, switch back to the main branch and discard these changes. First, switch back to the main branch:

    shell
    git checkout main

    Then, use the following command to discard uncommitted changes:

    shell
    git reset --hard

    This will reset the main branch to the last commit state, discarding all uncommitted changes. Note that this is a dangerous operation as it discards all uncommitted changes. Before using git reset --hard, ensure you don't need these uncommitted changes.

This is the basic process for moving uncommitted work to a new branch. Let's look at a specific example:

Suppose you are working on the main branch main and have made some changes. Now you want to move these changes to a new branch feature-x.

  1. Check changes:
    sh
    git status
  2. Create and switch to new branch feature-x:
    sh
    git checkout -b feature-x
  3. Stage all changes and commit them to the new branch:
    sh
    git add . git commit -m "Start working on feature X"
  4. If needed, switch back to main branch and discard changes:
    sh
    git checkout main git reset --hard

Now, the new branch feature-x contains the previously uncommitted work, while the main branch remains unchanged.

2024年6月29日 12:07 回复

If you've been working on the main branch while coding and now want to move these commits to a different branch, this is a quick method:

  1. Create a new branch with the current history and all uncommitted changes:

    git checkout -b <new-feature-branch>

  2. Now force reset the previous branch to an earlier state (without switching to it):

    git branch -f <previous-branch> <earlier-commit-id>

    For example:

    git branch -f master origin/master

    Or if you've made 4 commits:

    git branch -f master HEAD~4

Warning: git branch -f master origin/master will reset the tracking information of the branch. Therefore, if you have configured the master branch to push to another remote, the configuration for origin/master will be lost.

Warning: If you perform a rebase after branching, there is a risk of losing some commits, as described here [https://stackoverflow.com/a/36463546/99777]. The only way to avoid this is to create a new history using cherry-pick. The linked resource describes the safest and most reliable method, though it is less convenient. (If you have uncommitted changes, you may need to use git stash at the beginning and git stash pop at the end.)

2024年6月29日 12:07 回复

Stash the current changes:

  1. $ git stash
  2. Create and switch to a new branch based on this stash: $ git stash branch <new-branch> stash@{0}

Tip: Use the Tab key to autocomplete the stash name.

2024年6月29日 12:07 回复

你的答案