Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Check out a branch keeping uncommitted changes

Tags:

git

branch

In Git, how would I check out an older branch while preserving all my changes from the current branch, but they would just be marked as uncommitted changes or new files.

Is there a command like git checkout-but-keep-changes branch_name?

like image 566
Drew LeSueur Avatar asked Oct 10 '14 19:10

Drew LeSueur


People also ask

Can I checkout branch without commit?

you can do git checkout -m <branch-name> to merge conflicts and checkout to the branch and resolve conflicts yourself, or git checkout -f <branch-name> to ignore changes.

Can I change branch with uncommitted changes?

You may switch branches with uncommitted changes in the work-tree if and only if said switching does not require clobbering those changes.

What happens when you check out a branch?

Checking out branches Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch. Think of it as a way to select which line of development you're working on.

How do I create a branch without losing changes?

The git checkout -b <BranchName> command will create a new branch and switch to it. Moreover, this command will leave the current branch as it is and bring all uncommitted changes to the new branch. There is no local change on the master branch, as we can see in the output.

Why can’t I checkout another branch in Git?

Generally, Git won’t let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren’t committed. You have three options to handle your changes: 1) trash them, 2) commit them, or 3) stash them.

What happens when you checkout a commit in Git?

Internally, the git checkout command simply updates the HEAD to point to either the specified branch or commit. When it points to a branch, Git doesn't complain, but when you check out a commit, it switches into a “detached HEAD” state.

How to remove uncommitted changes in Git?

In summary, we have several approaches to remove uncommitted changes: git checkout is only useful when files are not in the staging area. git reset useful for changes that are in staging area but cannot remove changes on untracked files, requires a combination with git checkout.

How do I force a branch change in Git?

Force a Checkout. You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD). Basically, it can be used to throw away local changes. When you run the following command, Git will ignore unmerged entries:


1 Answers

Git normally does this when you check files out, assuming the changes you have made are not to files that are going to be changed by the checkout. For example, suppose I have the following files in a repository:

a.txt
b.txt
c.txt

Now I'll make a change to file a.txt:

a.txt (modified)
b.txt
c.txt

Now suppose I have two other branches, a-changed and b-changed, in which a.txt and b.txt have been modified, respectively.

If I try to check out b-changed, Git will do that without complaint, because my working directory changes don't conflict with the changes made on that branch. However, if I try to check out a-changed, Git won't do that, because I have changes made to my local copy, and the checkout would clobber them. Git says:

$ git checkout a-changed
error: Your local changes to the following files would be overwritten by checkout:
        a.txt
Please, commit your changes or stash them before you can switch branches.
Aborting

Git does provide you with a way to get around this problem: stash your changes! This saves your current changes as a patch and undoes them in the working directory. That is, after running git stash, the working directory will be cleaned, and your changes placed onto the stash stack. You can then checkout the desired branch and run git stash pop, which will pop your changes off the stack and apply them to the new working tree. Hopefully, they'll apply cleanly. If they don't, you'll get the standard merge conflict markers in a.txt. Notably, the stash is not popped off the stack, so you need to do a git stash drop after you resolve the merge.

You can also pass the -m (--merge) flag to the checkout command, which indicates that you would like your changes to be three-way merged with the incoming changes. Generally, I prefer stashing things.

like image 70
George Hilliard Avatar answered Sep 23 '22 02:09

George Hilliard