git-stash和git-checkout之间有什么区别?

16 浏览
0 Comments

git-stash和git-checkout之间有什么区别?

我试图从一个本地分支切换到另一个本地分支。Git告诉我不能这样做,因为我的本地更改会被切换覆盖。\n然后我得到了一个\"建议\"请在切换分支之前提交或隐藏您的更改。\n我知道我不需要对这个文件的更改。覆盖它们是可以的。所以,我尝试使用stash。我执行git stash file_name。结果是:\n用法:git stash list [<选项>]\n 或:git stash show []\n 或:git stash drop [-q|--quiet] []\n 或:git stash ( pop | apply ) [--index] [-q|--quiet] []\n 或:git stash branch []\n 或:git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]\n [-u|--include-untracked] [-a|--all] []]\n 或:git stash clear\n\n好的。它不起作用。然后我尝试git checkout file_name。Git没有投诉。然后我可以从一个分支切换到另一个分支。所以,看起来我得到了我需要的结果(在不保存对第一个分支的更改的情况下转到第二个分支)。\n然而,我想问一下为什么stash没有起作用,如果它起作用的话,最终结果会有什么不同?

0
0 Comments

Git中有两个命令可以在切换分支时保存未提交的更改,它们是git-stash和git-checkout。但是它们在保存和应用更改的方式上有一些不同。

首先,git stash命令可以将未提交的更改保存到一个未完成更改的堆栈中,而不进行提交。如果只需要保存一部分已完成的工作,可以使用git stash -p命令。要重新应用保存的更改,可以使用git stash pop命令。

而git checkout命令则有不同的用法。如果使用git checkout命令而不加任何参数,工作树将指向当前的HEAD。如果使用git checkout branchname命令切换到另一个分支,并且有未提交的更改,这些更改将作为修改后的更改添加到新切换的分支上。如果使用git checkout filepath命令并指定文件路径,该文件的未提交更改将会丢失。

需要注意的是,git checkout filepath或git checkout -- filepath命令只能对未暂存的更改生效。如果更改已经被暂存,使用checkout命令将不再有效(在这种情况下,我们不应该丢失已经暂存但尚未提交的更改)。如果希望取消暂存,我们需要使用git reset HEAD filepath命令。

git stash命令可以保存未提交的更改,并在需要时重新应用这些更改,而git checkout命令则可以在切换分支时将未提交的更改添加到新的分支上或丢失未提交的更改。根据具体情况选择使用哪个命令可以更好地管理和保存代码更改。

0
0 Comments

git checkout is used to switch branches or restore files from a specific commit. It can also be used to create a new branch.

The main difference between git stash and git checkout is that git stash is used to save changes that are not ready to be committed, while git checkout is used to switch branches or restore files.

When we have made changes to our code but are not ready to commit them yet, we can use git stash to save these changes temporarily. This is useful when we need to switch to a different branch or work on a different task without committing the changes we have made.

To use git stash, we can run the command "git stash save". This will save our changes into a stash, which is like a stack of temporary commits. We can view the list of stashes with the command "git stash list".

In some cases, we may only want to stash certain files or even specific chunks of code. We can do this by using the command "git stash save -p". This will interactively ask us what we want to save.

Another way to use git stash is to add all files except one to the index with "git add", and then run "git stash save -k". This will stash the changed file without including the files that are prepared to be committed.

On the other hand, git checkout is mainly used to switch branches or restore files from a specific commit. To switch to a different branch, we can run the command "git checkout branch_name". This will switch our working directory to the specified branch.

We can also use git checkout to restore files from a specific commit. For example, if we want to restore a file named "filename.txt" from a commit with the hash "commit_hash", we can run the command "git checkout commit_hash -- filename.txt". This will replace the current version of the file with the version from the specified commit.

In addition, git checkout can be used to create a new branch. We can run the command "git checkout -b new_branch_name" to create a new branch and switch to it at the same time.

In conclusion, the main difference between git stash and git checkout is that git stash is used to save changes temporarily without committing them, while git checkout is used to switch branches or restore files from a specific commit. Both commands are useful in different scenarios and can help us manage our code changes effectively.

0
0 Comments

git-stash和git-checkout之间的区别是什么?

在git中,通过git-stash命令可以将所有修改都暂存起来,而无法单独暂存一个文件。而通过git checkout -- file命令可以丢弃对该文件的修改,并将工作目录中的该文件恢复到当前提交记录中所记录的版本(即HEAD指向的版本)。

为什么git提供了暂存所有修改但没有提供暂存单个文件的可能性?难道stash不是“丢弃我的对工作目录中的文件的修改,并检出该文件的最新提交版本”吗?

实际上,你可以使用git stash -p命令来暂存任意部分。类似于git add -p命令可以让你添加或拒绝每个代码块,git stash -p命令可以让你选择性地暂存修改的部分。具体可参考这里的说明。

需要注意的是,“最新”的表述并不完全准确,这可能会引起误解。正确的表述应该是“当前提交记录中所记录的版本”(即HEAD指向的版本)。

从Git 2.13版本开始,现在可以单独暂存文件了。具体可参考stackoverflow.com/a/5506483/1743874

0