This article is part of a follow-along series on GitHub collaboration. In this article, we will try to understand commits in detail, like components of a commit, how to delete commit before and after push or how to reset it.
本文是有关GitHub协作的后续系列的一部分。 在本文中,我们将尝试详细了解提交,例如提交的组件,如何在推送之前和之后删除提交或如何重置提交。
If you are a beginner and want to understand the practical application, I will recommend a quick read into the article, Collaborate on GitHub like Pro: Part1 before you start with this article.
如果您是初学者,并且想了解实际的应用程序,那么在开始本文之前,我建议您快速阅读一下文章,像Pro: Part1一样在GitHub上进行协作。
The series, Collaborate on GitHub like pro, focus on specific topics:
该系列在GitHub上像pro一样在GitHub上合作,重点关注以下特定主题:
Getting started on GitHub: Collaborate on GitHub like Pro: Part1
GitHub入门 : 像Pro: Part1一样 在GitHub上进行 协作
Branching: Collaborate on GitHub like Pro: Part2
分支: 像Pro: Part2一样在GitHub上进行协作
Commit: Collaborate on GitHub like Pro: Commit
提交:像Pro: Commit一样在GitHub上进行协作
Commits are created with the git commit command to capture the state of a project at that point in time. When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the content you staged (indexed), please see below commit stage highlighted in red box.
使用git commit命令创建提交,以捕获该时间点的项目状态。 当您进行提交时,Git将存储一个提交对象,该对象包含一个指向您已暂存(索引)内容快照的指针,请参见下面的提交阶段以红色框突出显示。
Commit flowchart 提交流程图Let us take a look into the commit object, shown below. I have highlighted the three main components in the commit, hash, head and branch (master).
让我们看一下提交对象,如下所示。 我已经强调了commit, hash , head和branch (master)中的三个主要组成部分。
Commit Object 提交对象Hash: Every commit creates unique hash for the respective commits. These hashes can later be used to revert to that version or find details on the commit. Usually, only the first 7 characters are used to look for respective commit.
哈希 :每个提交都会为各自的提交创建唯一的哈希。 这些哈希值以后可以用于还原到该版本或查找有关提交的详细信息。 通常,仅使用前7个字符来查找各自的提交。
Head: Shows which branch you are working on currently. In the above image, head is pointing to master, which means currently you are working on the master branch.
标题 :显示您当前正在哪个分支。 在上图中,head指向master,这意味着您当前正在master分支上工作。
Branch: By default, the first line of development (branch) is named as master. All the preceding work on different branches gets merged to master.
分支:默认情况下,开发的第一行(分支)命名为master。 先前在不同分支上的所有工作都合并到master中。
To understand this concept further refer to this blog post.
要了解此概念,请进一步参阅此博客文章 。
Master branch after three commits 三次提交后的Master分支There are multiple ways to checkout to a specific commit based on the use cases. You might want to make a temporary or permanent switch. Sometimes you might want to go a few steps back and maybe add a feature from that step onwards.
根据用例,有多种方法可以签出到特定的提交。 您可能需要进行临时或永久切换。 有时您可能想往后退几步,并可能从此步骤开始添加功能。
If you want to switch to the first commit (7d8d1af) temporarily and make few commits from the specific commit, run these commands.
如果要临时切换到第一个提交(7d8d1af),并从特定提交中进行很少的提交,请运行以下命令。
$ cd autos/ (git project root)$ git checkout 7d8d1af (Use hash if you know it, else)$ git checkout HEAD~3 (Use number of commits to go back, our case 3)$ git checkout -b new-branch 7d8d1af (create new branch on the commit)$ git checkout branch_name (to go back to respective branch)When you checkout of a commit, your head gets detached. Detached head means that the head is not pointing to the recent commit instead to your checkout commit.
当您签出提交时,您的头会分离。 分离的头部意味着头部不指向最近的提交,而是指向您的结帐提交。
Temporary commit checkout flow 临时提交结帐流程Git reset option can be used to delete a commit permanently. Git reset has 5 main modes: soft, mixed, merged, hard, keep. For details and different solutions refer to stackoverflow.
Git reset选项可用于永久删除提交。 Git重置有5种主要模式: 软,混合,合并,硬,保持 。 有关详细信息和不同的解决方案,请参阅stackoverflow 。
Git reset — hard will change head, index (stage) and working directory. Always remember to run git status to check if the working tree is clean, else you may lose all the uncommitted changes.
Git重置-很难更改头,索引(阶段)和工作目录。 始终记得运行git status来检查工作树是否干净,否则您可能会丢失所有未提交的更改。
Git reset — soft will change head, no change to index (stage) or working directory. It is the safest option.
Git重置—软件将更改头,而不更改索引(阶段)或工作目录。 这是最安全的选择。
For example, we would like to reset to the first commit (7d8d1af). There are two ways to handle the situation.
例如,我们想重置为第一次提交(7d8d1af)。 有两种方法可以处理这种情况。
Commit NOT pushed to remote
提交未推送到远程
Always run git status to check if the working tree is clean. If you want to merely delete the current commit but leave files and index as it is, you will use soft.
始终运行git status来检查工作树是否干净。 如果您只想删除当前提交但不保留文件和索引,则可以使用soft。
$ cd autos/ (git project root)$ git reset --soft HEAD~3If your working tree is clean, you know there is no uncommitted work that you can lose. In that case, you can safely use hard.
如果您的工作树是干净的,那么您就知道不会丢失任何未完成的工作。 在这种情况下,您可以放心使用。
$ cd autos/ (git project root)$ git reset --hard 7d8d1af (Use hash if you know it, else)$ git reset HEAD~3 (Use number of commits to go back, our case 3)In the case where your working tree is NOT clean, you know there are some uncommitted work that you can lose if you use hard directly. It is recomended to run stash to save your local changes before the command hard.
在工作树不干净的情况下,您知道有一些未承诺的工作,如果直接使用辛苦,可能会丢失。 建议在执行命令之前先运行stash来保存本地更改。
$ cd autos/ (git project root)$ git stash$ git reset --hard 7d8d1af$ git stash pop2. Commit pushed to remote
2.提交推送到远程
$ git revert --no-commit 7d8d1af HEAD$ git commit$ git push# To abort the above stepsgit revert --abortTo learn more about git reset/revert and return to previous states in Git, refer to this well-written blog.
要了解有关git reset / revert并返回到Git中以前状态的更多信息,请参考这个写得很好的博客 。
The codes for this project can be found in the GitHub repository.
该项目的代码可以在GitHub存储库中找到。
https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit/4114122#4114122
https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit/4114122#4114122
翻译自: https://towardsdatascience.com/collaborate-on-github-like-a-pro-commit-d832e613b109