简体   繁体   English

如何切换到另一个 git 分支并且不要将我当前的文件移动到该分支

[英]How do I switch to another git branch and don't move my current files with me to that branch

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 .假设我在分支 B 上有 10 个文件,我在第 2 阶段,在第 1 阶段的分支 A 上有 5 个文件我想从分支 B 切换到分支 A,但只是为了在那个阶段查看分支 A 的原始文件希望我的文件和分支 B 的进度与我一起移动。 how can I do that?我怎样才能做到这一点?

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).您可以使用 git stash 在其中基本上记录工作目录和索引的当前状态,但能够返回到干净的工作目录(您想要做什么)。 It saves your local modification away.它可以保存您的本地修改。 You can find more information here: https://git-scm.com/docs/git-stash您可以在此处找到更多信息: 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] . git checkout [修订] 。

  • where the revision is the commit hash - it looks something like this: 12345678901234567890123456789012345678ab.其中修订版是提交哈希 - 它看起来像这样:12345678901234567890123456789012345678ab。 Don't forget to add the dot at the end.不要忘记在末尾添加点。

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.每个提交(上面表示中的一个点 - o -)都有一个哈希号,分支名称实际上只是分支最后一次提交的哈希的别名,因为Branch-B比记住要容易得多每个新提交的哈希值都像0f5c9409fd9dbe02217e1f9ea71e732704748a91

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如果你想在树的另一个点浏览文件,你可以用`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 checkout出现错误,说明您修改了尚未提交的文件,您可以:

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

  • Or you can save your current modification in a sort of "magic buffer" to retrieve it later with git stash .或者,您可以将当前修改保存在一种“魔术缓冲区”中,以便稍后使用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.运行git stash后,看起来您丢失了所有修改,但是一旦您返回同一分支,您可以使用git stash pop检索它。

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!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM