Switch to another branch without changing the workspace files

GitBranchGit Branch

Git Problem Overview


I cloned a git repository from GitHub, made some changes and some commits; I made quite a lot and all are quite dirty, so they're not suitable for a pull request. Now I created the branch cleanchanges from origin/master, so it's clean, and I want to commit my changes there as one commit with a nice commit comment.

When I am on the local master, I want to switch to my cleanchanges but without changing the files. And then I'm able to commit.

How can I switch branches without changing files?

I want to make it clear: I have all the changes committed in the local master. There are no uncommitted changes.

Git Solutions


Solution 1 - Git

Edit: I just noticed that you said you had already created some commits. In that case, use git merge --squash to make a single commit:

git checkout cleanchanges
git merge --squash master
git commit -m "nice commit comment for all my changes"

(Edit: The following answer applies if you have uncommitted changes.)

Just switch branches with git checkout cleanchanges. If the branches refer to the same ref, then all your uncommitted changes will be preserved in your working directory when you switch.

The only time you would have a conflict is if some file in the repository is different between origin/master and cleanchanges. If you just created the branch, then no problem.

As always, if you're at all concerned about losing work, make a backup copy first. Git is designed to not throw away work without asking you first.

Solution 2 - Git

Git. Switch to another branch

git checkout branch_name

Solution 3 - Git

The best bet is to stash the changes and switch branch. For switching branches, you need a clean state. So stash them, checkout a new branch and apply the changes on the new branch and commit it

Solution 4 - Git

Another way, if you want to create a new commit instead of performing a merge:

git checkout cleanchanges
git reset --hard master
git reset cleanchanges

git status
git add .
git commit

The first (hard) reset will set your working tree to the same as the last commit in master.

The second reset will put your HEAD back where it was, pointing to the tip of the cleanchanges branch, but without changing any files. So now you can add and commit them.


Afterwards, if you want to remove the dirty commits you made from master (and assuming you have not already pushed them), you could:

git checkout master
git reset --hard origin/master

This will discard all your new commits, returning your local master branch to the same commit as the one in the repository.

Solution 5 - Git

Why not just git reset --soft <branch_name>?

Demonstration:

mkdir myrepo; cd myrepo; git init
touch poem; git add poem; git commit -m 'add poem'  # first commit
git branch original
echo bananas > poem; git commit -am 'change poem'  # second commit
echo are tasty >> poem  # unstaged change
git reset --soft original

Result:

$ git diff --cached
diff --git a/poem b/poem
index e69de29..9baf85e 100644
--- a/poem
+++ b/poem
@@ -0,0 +1 @@
+bananas
$ git diff
diff --git a/poem b/poem
index 9baf85e..ac01489 100644
--- a/poem
+++ b/poem
@@ -1 +1,2 @@
 bananas
+are tasty

One thing to note though, is that the current branch changes to original. You’re still left in the previous branch after the process, but can easily git checkout original, because it’s the same state. If you do not want to lose the previous HEAD, you should note the commit reference and do git branch -f <previous_branch> <commit> after that.

Solution 6 - Git

It sounds like you made changes, committing them to master along the way, and now you want to combine them into a single commit.

If so, you want to rebase your commits, squashing them into a single commit.

I'm not entirely sure of what exactly you want, so I'm not going to tempt you with a script. But I suggest you read up on git rebase and the options for "squash"ing, and try a few things out.

Solution 7 - Git

Cleanly switching to another branch cleanchanges with current workspace files preserved - without side effects or intermediate commits:

# starting from branch master
git switch --detach               # don't move the current branch
git reset cleanchanges
git switch cleanchanges

This allows even uncommited changes, and doesn't change the timestamps of files.

In order to also preserve the index (staging area) and thus see the diffs of dirty changes separately, do like this:

# starting from master
git switch --detach               # don't move the current branch
git reset --soft cleanchanges
git switch cleanchanges

Solution 8 - Git

Simplest way to do is as follows:

git fetch && git checkout <branch_name>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionamorfisView Question on Stackoverflow
Solution 1 - GitGreg HewgillView Answer on Stackoverflow
Solution 2 - GitSurendra Kumar AhirView Answer on Stackoverflow
Solution 3 - Gitvivek85View Answer on Stackoverflow
Solution 4 - GitjoeytwiddleView Answer on Stackoverflow
Solution 5 - GitYushin WashioView Answer on Stackoverflow
Solution 6 - GittimdevView Answer on Stackoverflow
Solution 7 - GitkxrView Answer on Stackoverflow
Solution 8 - GitMandeep SinghView Answer on Stackoverflow