Home > Blockchain >  How do I switch to another git branch and don't move my current files with me to that branch
How do I switch to another git branch and don't move my current files with me to that branch

Time:05-28

I am on a ahead branch and want to check my old files at older branch but when I switch I move my files and progress with me how can i prevent that from happening? lets say I have 10 files on branch B and I am at stage 2, and 5 files on branch A on stage 1 I want to switch to branch A from branch B but just to see branch A's original files at that stage and don't want my files and progress from branch B to move with me . how can I do that?

CodePudding user response:

You can do 2 things:

you can use git stash where you basically record the current state of the working directory and the index, but are able to go back to a clean working directory (what you want to do). It saves your local modification away. You can find more information here: https://git-scm.com/docs/git-stash

alternative: you also checkout specific commits so you can easily go back to what you committed on each branch

git checkout [revision] .

  • where the revision is the commit hash - it looks something like this: 12345678901234567890123456789012345678ab. Don't forget to add the dot at the end.

CodePudding user response:

just to make sure I understood correctly, let's say you have the following commit tree (new commit are appended on the branch on the right of the representation below):

       - o - o - o - o - o -> Branch-A
      /
o -o -o - o - o - o - o - o -> Branch-B

Each point of the tree can have completely different files and folders.

Lets say you are in one point of this tree and want to see files from another point of the tree without switching to that point, you can do:

git show branch_or_hash:file

Examples:

git show Branch-B:my-file.txt
git show 0f5c9409fd9dbe02217e1f9ea71e732704748a91:my-file.txt

Every commit (a dot - o - in the representation above) has a hash number, the branch name is in practice just an alias to the hash of the last commit of a branch, because Branch-B is much easier to remember instead of remembering the hash like 0f5c9409fd9dbe02217e1f9ea71e732704748a91 for every new commit.

With the command above you are viewing a specific file, but you need to know that file exists there in advance.

If you want to navigate through the files in another point of the tree, you can freely switch back and forth with `git checkout branch_or_hash

Examples:

git checkout Branch-A
# now you can navigate through all the files in Branch-A
git checkout Branch-B
# now you can navigate through all the files in Branch-B, files from Branch-A won't be visible anymore
git checkout 0f5c9409fd9dbe02217e1f9ea71e732704748a91
# now you can navigate through all this file in this specific point of the tree
git checkout Branch-A
# now you are back to Branch-A as nothing has even happened

Now, if your git checkout gives an error saying you have modified files that are not in any commit yet, you can:

  • git add file && git commit -m "modified my file" and you will be able to git checkout

  • Or you can save your current modification in a sort of "magic buffer" to retrieve it later with git stash. After running git stash it will look like you lost all your modifications, but you can retrieve it with git stash pop once you return to the same branch.

echo "foo" >> my-file.txt
git checkout Branch-B # you will get an error
git status # you can see which files got modified
git add my-file.txt
git commit -m "add foo to my-file.txt"
git checkout Branch-B # it works!
git checkout Branch-A # it works! Back to Branch-A again!

# Lets try with the second option
echo "bar" >> my-file.txt
git checkout Branch-B # you will get an error
git stash
# now if you check the content of my-file.txt, it will look like you lost all your modifications, the word "bar" won't be there
git checkout Branch-B # it works!
git checkout Branch-A # it works! Back to Branch-A again!
git stash pop # retrieve your modifications back

I hope this helps!

  •  Tags:  
  • git
  • Related