git重置_Git重置和Git还原终极指南

git重置

Welcome to our ultimate guide to the git reset and git revert commands. This tutorial will teach you everything you need to know about fixing common mistakes and undoing bad commits while using Git.

欢迎来到git resetgit revert命令的最终指南。 本教程将教您使用Git时解决常见错误和撤消错误提交所需的一切。

了解Git项目的三个部分 (Understand the Three Sections of a Git Project)

A Git project has the following three main sections:

Git项目包含以下三个主要部分:

  1. Git directory

    Git目录
  2. Working directory (or working tree)

    工作目录(或工作树)
  3. Staging area

    暂存区

The Git directory (located in YOUR-PROJECT-PATH/.git/) is where Git stores everything it needs to accurately track the project. This includes metadata and an object database which includes compressed versions of the project files.

Git目录 (位于YOUR-PROJECT-PATH/.git/ )是Git存储准确跟踪项目所需的所有内容的位置。 这包括元数据和一个对象数据库,其中包含项目文件的压缩版本。

The working directory is where a user makes local changes to a project. The working directory pulls the project's files from the Git directory's object database and places them on the user's local machine.

用户在工作目录中对项目进行本地更改。 工作目录从Git目录的对象数据库中提取项目的文件,并将其放置在用户的本地计算机上。

Note: Directory is also known as Repository or short form repo. The repo on the user's local machine is called "Local repo" while the repo on git server is called "Remote repo".

注意: 目录也称为存储库或短格式存储库 。 用户本地计算机上的存储库称为“本地存储库”,而git服务器上的存储库称为“远程存储库”。

The staging area is a file (also called the "index", "stage", or "cache") that stores information about what will go into your next commit. A commit is when you tell Git to save these staged changes. Git takes a snapshot of the files as they are and permanently stores that snapshot in the Git directory.

暂存区域是一个文件(也称为“索引”,“阶段”或“缓存”),用于存储有关下一次提交内容的信息。 提交是指您告诉Git保存这些阶段的更改。 Git照原样拍摄文件快照,并将该快照永久存储在Git目录中。

With three sections, there are three main states that a file can be in at any given time: modified, committed, or staged. You modify a file any time you make changes to it in your working directory. Next, it's staged when you move it to the staging area. Finally, it's committed after a commit.

在三个部分中,文件可以在任何给定时间处于三种主要状态:已修改,已提交或已暂存。 您修改一个文件您在您的工作目录更改它的任何时间。 其次,这是当你将它移动到临时区域上演 。 最后,在提交之后提交。

Git重置 (Git Reset)

The git reset command allows you to RESET your current head to a specified state. You can reset the state of specific files as well as an entire branch. This is useful if you haven't pushed your commit up to GitHub or another remote repository yet.

git reset命令允许您将当前磁头重置为指定状态。 您可以重置特定文件以及整个分支的状态。 如果您尚未将提交推送到GitHub或另一个远程存储库,这将很有用。

重置一个文件或一组文件 (Reset a file or set of files)

The following command lets you selectively choose chunks of content and revert or unstage it.

以下命令使您可以有选择地选择大块内容并还原或取消登台。

git reset (--patch | -p) [tree-ish] [--] [paths]

取消暂存文件 (Unstage a file)

If you moved a file into the staging area with git add, but no longer want it to be part of a commit, you can use git reset to unstage that file:

如果您使用git add文件移到暂存区域,但不再希望其成为提交的一部分,则可以使用git reset取消git reset该文件:

git reset HEAD FILE-TO-UNSTAGE

The changes you made will still be in the file, this command just removes that file from your staging area.

您所做的更改仍将保留在文件中,此命令只是从暂存区中删除该文件。

将分支重置为先前的提交 (Reset a branch to a prior commit)

The following command resets your current branch's HEAD to the given COMMIT and updates the index. It basically rewinds the state of your branch, then all commits you make going forward write over anything that came after the reset point. If you omit the MODE, it defaults to --mixed:

以下命令将当前分支的HEAD重置为给定的COMMIT并更新索引。 它基本上倒退了分支的状态,然后进行的所有提交都将覆盖重置点之后的所有内容。 如果省略MODE ,则默认为--mixed

git reset MODE COMMIT

The options for MODE are:

MODE的选项为:

  • --soft: does not reset the index file or working tree, but resets HEAD to commit. Changes all files to "Changes to be commited"

    --soft :不重置索引文件或工作树,但将HEAD重置为commit 。 将所有文件更改为“要提交的更改”

  • --mixed: resets the index but not the working tree and reports what has not been updated

    --mixed :重置索引,但不重置工作树,并报告尚未更新的内容

  • --hard: resets the index and working tree. Any changes to tracked files in the working tree since commit are discarded

    --hard :重置索引和工作树。 自commit以来对工作树中跟踪文件的任何更改都将被丢弃

  • --merge: resets the index and updates the files in the working tree that are different between commitand HEAD, but keeps those which are different between the index and working tree

    --merge :重置索引并更新工作树中commit和HEAD之间不同的文件,但保留那些索引和工作树之间不同的文件

  • --keep: resets index entries and updates files in the working tree that are different between commitand HEAD. If a file that is different between commit and HEAD has local changes, the reset is aborted

    --keep :重置索引条目并更新工作树中commit和HEAD之间不同的文件。 如果commit和HEAD之间的文件不同,则具有本地更改,则重置将中止

关于硬重置的重要说明 (Important Note About Hard Resets)

Be very careful when using the --hard option with git reset since it resets your commit, staging area and your working directory. If this option is not used properly then one can end up losing the code that is written.

git reset使用--hard选项时要非常小心,因为它会重置提交,暂存区和工作目录。 如果未正确使用此选项,则可能最终会丢失所写的代码。

Git还原 (Git Revert)

Both the git revert and git reset commands undo previous commits. But if you've already pushed your commit to a remote repository, it is recommended that you do not use git reset since it rewrites the history of commits. This can make working on a repository with other developers and maintaining a consistent history of commits very difficult.

git revertgit reset命令都撤消先前的提交。 但是,如果您已经将提交推送到远程存储库,则建议您不要使用git reset因为它会重写提交的历史记录。 这可能会使与其他开发人员一起在存储库上工作,并且很难保持一致的提交历史。

Instead, it is better to use git revert, which undoes the changes made by a previous commit by creating an entirely new commit, all without altering the history of commits.

相反,最好使用git revert ,它通过创建一个全新的提交来撤消上一次提交所做的更改,而所有这些都不会改变提交的历史记录。

还原一个或一组提交 (Revert a commit or set of commits)

The following command lets you revert changes from a previous commit or commits and create a new commit.

以下命令使您可以还原上一个或多个提交的更改并创建一个新的提交。

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…
git revert --continue
git revert --quit
git revert --abort

常用选项: (Common options:)

-e
  --edit
  • This is the default option and doesn't need to be explicitly set. It opens your system's default text editor and lets you edit the new commit message before commit the revert.

    这是默认选项,不需要显式设置。 它会打开系统的默认文本编辑器,并允许您在提交还原之前编辑新的提交消息。
  • This option does the opposite of -e, and git revert will not open the text editor.

    此选项与-e相反,并且git revert将不会打开文本编辑器。

  • This option prevents git revert from undoing a previous commit and creating a new one. Rather than creating a new commit, -n will undo the changes from the previous commit and add them to the Staging Index and Working Directory.

    此选项可防止git revert撤消先前的提交并创建新的提交。 -n会取消对先前提交的更改,而不是创建新的提交,并将其添加到暂存索引和工作目录中。

--no-edit
-n
-no-commit

例。 (Example.)

Let's imagine the following situation: 1.) You are working on a file and you add and commit your changes. 2.) You then work on a few other things, and make some more commits. 3.) Now you realize, three or four commits ago, you did something that you would like to undo - how can you do this?

让我们想象一下以下情况:1.)您正在处理文件,然后添加并提交更改。 2)然后,您还要处理其他一些事情,并进行更多提交。 3.)现在您意识到,三到四次提交之前,您做了一些想要撤消的操作-如何做到这一点?

You might be thinking, just use git reset, but this will remove all of the commits after the one you would like to change - git revert to the rescue! Let's walk through this example:

您可能会想,只需使用git reset ,但这将在您要更改的提交之后删除所有提交git revert救援! 让我们来看这个例子:

mkdir learn_revert # Create a folder called `learn_revert`
cd learn_revert # `cd` into the folder `learn_revert`
git init # Initialize a git repository

touch first.txt # Create a file called `first.txt`
echo Start >> first.txt # Add the text "Start" to `first.txt`

git add . # Add the `first.txt` file
git commit -m "adding first" # Commit with the message "Adding first.txt"

echo WRONG > wrong.txt # Add the text "WRONG" to `wrong.txt`
git add . # Add the `wrong.txt` file
git commit -m "adding WRONG to wrong.txt" # Commit with the message "Adding WRONG to wrong.txt"

echo More >> first.txt # Add the text "More" to `first.txt`
git add . # Add the `first.txt` file
git commit -m "adding More to first.txt" # Commit with the message "Adding More to first.txt"

echo Even More >> first.txt # Add the text "Even More" to `first.txt`
git add . # Add the `first.txt` file
git commit -m "adding Even More to First.txt" # Commit with the message "Adding More to first.txt"

# OH NO! We want to undo the commit with the text "WRONG" - let's revert! Since this commit was 2 from where we are not we can use git revert HEAD~2 (or we can use git log and find the SHA of that commit)

git revert HEAD~2 # this will put us in a text editor where we can modify the commit message.

ls # wrong.txt is not there any more!
git log --oneline # note that the commit history hasn't been altered, we've just added a new commit reflecting the removal of the `wrong.txt`

And with that you're one step closer to getting your black belt in Git.

这样一来,您就可以将黑带安装到Git中了。

翻译自: https://www.freecodecamp.org/news/the-ultimate-guide-to-git-reset-and-git-revert/

git重置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值