GIT: Adding Local Changes to Non-Current Branch

It usually happens to me that I make some local changes, only to discover that I made it on the wrong branch, so I need to switch branch before committing. The problem is that I cannot switch branch when there are local changes. Is there anyway to do that?

Obviously, I can copy the updated files, switch branch, then copy them back, but this doesn’t really seem clever!

  • moving commits from unrelated repository in git, git-p4
  • Git shallow fetch of a new tag
  • why doesn't work the `--includes` key of `git config` command for me?
  • Not able to push after resolving merge conflicts
  • Migrating Git hosts
  • SSL certificate rejected trying to access GitHub over HTTPS behind firewall
  • Git: how to roll back to last push/commit
  • Why doesn't “git status” display unpushed commits in a branch?
  • Can't push or pull to Github
  • Checking specific folders when git on Jenkins
  • Cannot remove git repository completely
  • Error after commit on git repository Android application
  • 3 Solutions collect form web for “GIT: Adding Local Changes to Non-Current Branch”

    You can switch branches while you have local modifications unless your local changes conflict with the difference between the two branches. In this case you can use the -m or --merge option to checkout to perform the checkout anyway and perform a merge betwee changes and the changes caused by switching branches.

    git checkout -m other-branch
    

    I use git stash when this happens. It creates a temporary commit of the current state of the working copy (both cached and uncached files) and reverts the working copy to the current HEAD. Then you can switch to the other branch and do git stash pop.

    As said by other you can use stash or checkout --merge. Those option however will cause a change in the timestamp of some file. If you’re working on a large project where compilation can take a long time (our current project take half an hour to compile with distributed builds), this may not be optimal.

    In this situation, you can use another repository to move the commit to the correct branch. First, you’ll need to clone your current repository (this need to be done only once):

    $ git clone /path/to/repository repository.clone
    $ cd repository.clone
    $ git remote add origin repository.clone
    $ git fetch origin
    

    Then in your current repository, you commit your changes:

    $ cd /path/to/repository
    $ git add path/to/modified/files
    $ git commit -m 'Commit message'
    

    On the other repository, you fetch the new commit, and move it to the correct branch:

    $ cd ../repository.clone
    $ git fetch origin
    $ git checkout correct-branch
    $ git reset --hard origin/correct-branch
    $ git cherry-pick origin/current-branch
    $ # resolve conflicts if any, commit with -c option in this case
    $ git push origin correct-branch:correct-branch
    

    Then on the original repository, you remove the temporary commit, and remove the associated modification (except if you want to keep them in both branches).

    $ cd /path/to/repository
    $ git reset HEAD^
    $ # edit file and remove modifications moved to other branch
    

    This is more complex and involve history rewriting, but when your project are really large, and compilation time is a limiting factor, it can be great to know the technique. Note that you can reuse the cloned repository, so there is no need to delete / recreate it each time (if compilation time is long, then repository is probably large, and cloning it can take some time).

    Git Baby is a git and github fan, let's start git clone.