1. git reset
NAME
git-reset - Reset current HEAD to the specified state
SYNOPSIS
git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
DESCRIPTION
set the current branch head (HEAD
) to <commit>
, optionally modifying index and working tree to match.
git reset [<mode>] [<commit>]
This form resets the current branch head to <commit>
and possibly updates the index (resetting it to the tree of <commit>
) and the working tree depending on <mode>
. If <mode>
is omitted, defaults to --mixed
. The <mode>
must be one of the following:
--soft
Does not touch the index file or the working tree at all (but resets the head to <commit>
, just like all modes do). This leaves all your changed files "Changes to be committed", as git status
would put it.
--mixed
Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
If -N
is specified, removed paths are marked as intent-to-add (see git-add(1)).
--hard
Resets the index and working tree. Any changes to tracked files in the working tree since <commit>
are discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted.
--merge
Resets the index and updates the files in the working tree that are different between <commit>
and HEAD
, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added). If a file that is different between <commit>
and the index has unstaged changes, reset is aborted.
In other words, --merge
does something like a git read-tree -u -m <commit>
, but carries forward unmerged index entries.
--keep
Resets index entries and updates files in the working tree that are different between <commit>
and HEAD
. If a file that is different between <commit>
and HEAD
has local changes, reset is aborted.
--[no-]recurse-submodules
When the working tree is updated, using --recurse-submodules will also recursively reset the working tree of all active submodules according to the commit recorded in the superproject, also setting the submodules' HEAD to be detached at that commit.
See "Reset, restore and revert" in git(1) for the differences between the three commands.
总结:
reset 可以用来恢复历史版本commit
加上--hard参数,将暂存区与工作区的内容都“重置”至commit状态。
reset的影响范围包括当前git版本目录下的所有文件和目录,不能单独恢复单个文件。
注意:
对于非可跟踪的文件和目录(即未git add 进来的文件或文件夹),保持最新状态,不作任何改变。
例如:
手动新增了一个文件newfile.txt,reset之后,newfile.txt不会被删除,因为newfile.txt未被git add,它是不可跟踪的。
2. git restore
NAME
git-restore - Restore working tree files
SYNOPSIS
git restore [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>…
git restore [<options>] [--source=<tree>] [--staged] [--worktree] --pathspec-from-file=<file> [--pathspec-file-nul]
git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>…]
DESCRIPTION
Restore specified paths in the working tree with some contents from a restore source. If a path is tracked but does not exist in the restore source, it will be removed to match the source.
The command can also be used to restore the content in the index with --staged
, or restore both the working tree and the index with --staged --worktree
.
By default, if --staged
is given, the contents are restored from HEAD
, otherwise from the index. Use --source
to restore from a different commit.
总结:
checkout的功能很多,从 Git 2.23
版本开始引入了两个新的命令: git switch
用来切换分支,git restore
用来还原工作区的文件。
restore可以分别对暂存区或工作区进行操作。
使用--staged参数,可以单独只恢复暂存区,也就是丢弃add到暂存区的状态,是将内容从commit恢复到暂存区,此时不影响工作区的内容,commit ----> 暂存区,如需进一步恢复工作区的内容,可以进一步执行git restore --worktree .;
使用--worktree,可以单独只恢复工作区,是将内容从暂存区恢复到工作区,而不是从commit恢复内容到工作区,暂存区 ----> 工作区!
也可以同时使用--staged --worktree,同时恢复暂存区和工作区,效果等同于git reset --hard.
也可以单独恢复git版本目录下的单独某一个文件。
注意:
对于非可跟踪的文件和目录(即未git add 进来的文件或文件夹),保持最新状态,不作任何改变。
例如:
手动新增了一个文件newfile.txt,reset之后,newfile.txt不会被删除,因为newfile.txt未被git add,它是不可跟踪的。
3. git checkout
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.
OPTIONS
-f
--force
When switching branches, proceed even if the index or the working tree differs from HEAD
, and even if there are untracked files in the way. This is used to throw away local changes and any untracked files or directories that are in the way.
When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored.
总结:
1. checkout 可以用来切换分支branch,等同于git branch.
2. checkout也可以用来恢复暂存区和工作区内容,等同于git reset
3. 切换版本:git checkout -f <COMMIT> 等同于git reset --hard <COMMIT>
-f参数 强制切换
注意:
身兼数职的 git checkout
命令现在可以轻松一些了,从 Git 2.23
版本开始引入了两个新的命令: git switch
用来切换分支,git restore
用来还原工作区的文件。
对于非可跟踪的文件和目录(即未git add 进来的文件或文件夹),保持最新状态,不作任何改变。
例如:
手动新增了一个文件newfile.txt,reset之后,newfile.txt不会被删除,因为newfile.txt未被git add,它是不可跟踪的。
4. git clean
NAME
git-clean - Remove untracked files from the working tree
SYNOPSIS
git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…
DESCRIPTION
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
Normally, only files unknown to Git are removed, but if the -x
option is specified, ignored files are also removed. This can, for example, be useful to remove all build products.
If any optional <path>...
arguments are given, only those paths are affected.
OPTIONS
-d
Normally, when no <path> is specified, git clean will not recurse into untracked directories to avoid removing too much. Specify -d to have it recurse into such directories as well. If any paths are specified, -d is irrelevant; all untracked files matching the specified paths (with exceptions for nested git directories mentioned under --force
) will be removed.
-f
--force
If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.
-i
--interactive
Show what would be done and clean files interactively. See “Interactive mode” for details.
-n
--dry-run
Don’t actually remove anything, just show what would be done.
-q
--quiet
Be quiet, only report errors, but not the files that are successfully removed.
-e <pattern>
--exclude=<pattern>
Use the given exclude pattern in addition to the standard ignore rules (see gitignore(5)).
-x
Don’t use the standard ignore rules (see gitignore(5)), but still use the ignore rules given with -e
options from the command line. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git restore or git reset) to create a pristine working directory to test a clean build.
-X
Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
总结:
清除git版本库下的所有非可跟踪的文件和目录,可以包括新增的文件和目录。
-f 强制删除,清除时只包括当前目录下的文件,不包括新增的目录或次级目录下的任意文件,可以使用-f -d 参数,将所有新增的文件和目录全部删除。
-d 清除时包括目录一起删除。
5. git revert
NAME
git-revert - Revert some existing commits
SYNOPSIS
git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…
git revert (--continue | --skip | --abort | --quit)
DESCRIPTION
Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).
Note: git revert is used to record some new commits to reverse the effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see git-reset(1), particularly the --hard
option. If you want to extract specific files as they were in another commit, you should see git-restore(1), specifically the --source
option. Take care with these alternatives as both will discard uncommitted changes in your working directory.
See "Reset, restore and revert" in git(1) for the differences between the three commands.
OPTIONS
<commit>…
Commits to revert. For a more complete list of ways to spell commit names, see gitrevisions(7). Sets of commits can also be given but no traversal is done by default, see git-rev-list(1) and its --no-walk
option.
-e
--edit
With this option, git revert will let you edit the commit message prior to committing the revert. This is the default if you run the command from a terminal.
-m parent-number
--mainline parent-number
Usually you cannot revert a merge because you do not know which side of the merge should be considered the mainline. This option specifies the parent number (starting from 1) of the mainline and allows revert to reverse the change relative to the specified parent.
Reverting a merge commit declares that you will never want the tree changes brought in by the merge. As a result, later merges will only bring in tree changes introduced by commits that are not ancestors of the previously reverted merge. This may or may not be what you want.
See the revert-a-faulty-merge How-To for more details.
--no-edit
With this option, git revert will not start the commit message editor.
--cleanup=<mode>
This option determines how the commit message will be cleaned up before being passed on to the commit machinery. See git-commit(1) for more details. In particular, if the <mode> is given a value of scissors
, scissors will be appended to MERGE_MSG
before being passed on in the case of a conflict.
-n
--no-commit
Usually the command automatically creates some commits with commit log messages stating which commits were reverted. This flag applies the changes necessary to revert the named commits to your working tree and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index.
This is useful when reverting more than one commits' effect to your index in a row.
总结:
改变之前的commit中的一些内容,并自动生成一个新的commit。