git reset和git checkout有什么不同?

你好。
前几天我不知道这两个标题的区别,所以进行了一些调查。
因为我从未在实务中使用过,所以理解可能有些误差,但这是为了总结目前的理解。
此外,为了不使文章混乱,我在写作过程中有意识地简明扼要。

在哪里使用?

这两个命令用于将索引或工作区中的某个内容恢复到某个状态。

1. 对于①和②,分别出现了下面的候选项。

2. 关于①和②,分别有以下候选方案出现。

3. 就①和②而言,各自出现了下列的候选。

4. 有关于①和②,分别出现了以下的候选选项。

5. 对于①和②来说,各有下面的候选出现。

– 仅特定的文件
– 所有的文件

头或索引
过去的提交

当你用git help命令查看各个命令时

git reset –帮助

NAME
       git-reset - Reset current HEAD to the specified state

SYNOPSIS
       git reset [-q] [<tree-ish>] [--] <pathspec>...
       git reset [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
       git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>...]
       git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

DESCRIPTION
       In the first three forms, copy entries from <tree-ish> to the index. In the last form, set the
       current branch head (HEAD) to <commit>, optionally modifying index and working tree to match. The
       <tree-ish>/<commit> defaults to HEAD in all forms.

总结起来,

    1. 将HEAD中的内容拷贝到索引中,并选择性地将其拷贝到工作目录中。

 

    将HEAD指向指定的提交,并选择性地从目标提交中将内容拷贝到索引和工作目录中。

简单来说,就是将当前的HEAD移动到指定的状态,并将其复制到索引和工作树中,根据选项选择。

    1. – hard:将变更同步到索引和工作树(注意,现有文件将与指定的HEAD同步)

 

    1. – mixed(默认):仅将变更同步到索引

 

    – soft:不进行变更同步(只有HEAD会移动)

请使用git checkout –help命令来获取帮助。

NAME
       git-checkout - Switch branches or restore working tree files

SYNOPSIS
       git checkout [-q] [-f] [-m] [<branch>]
       git checkout [-q] [-f] [-m] --detach [<branch>]
       git checkout [-q] [-f] [-m] [--detach] <commit>
       git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
       git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>...
       git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]
       git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]

DESCRIPTION
       Updates files in the working tree to match the version in the index or the specified tree. If no
       pathspec was given, git checkout will also update HEAD to set the specified branch as the current
       branch.

简而言之

    将工作树中的文件索引,然后将其与指定的提交版本匹配。

差異的關鍵詞 de cí)

    • reset

過去のコミットから反映させるとき
インデックスのみの変更

checkout

ワークツリーまで変更する。
ワークツリー・インデックス・HEADの3つを一致させる。
過去のコミットからの状態を「試したい」とき。

ブランチに影響を与えたくないとき。

用图表总结

スクリーンショット 2021-05-12 7.03.25.png

我觉得只要理解到这一步,接下来就只有实践了。
如果将来在实际工作中有更新的点,我会随时进行。
如果有在实际工作中使用过的人,我希望能得到其他地方或建议上的帮助。

以上。 .)

bannerAds