理解提交git? [英] understanding commits in git?

查看:137
本文介绍了理解提交git?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是一般的网络开发新手,所以如果我犯了任何明显的错误,请告诉我。



所以我决定,如果我想开始制作网站,我可能需要学习Git和GitHub。



我目前了解如何初始化存储库,阶段和提交并将其全部推送到GitHub页面。我现在正在努力的是恢复到旧提交的过程。所以可以说我已经做了两次提交,分别是commit 1,commit 2。



如果我在提交2中如何回去提交1?我可以回去提交一个更改代码提交更改,然后回到提交2后?在提交之间移动时,代码和文件会发生什么特殊情况。



请随时与我聊聊,就像一个5岁的孩子,他不理解Git和命令线。提前感谢大家。

解决方案

您可以使用 reset 浏览历史。看看它有多简单,首先让我们创建两个文件:

  $ touch a 
$ touch b

让我们提交其中一个:

  $ git add a 
$ git co -m第一次提交
[master d263314]第一次提交
1文件已更改,0次插入(+),0次删除 - )
创建模式100644 a

让我们提交另一个:

$ b
$ b

  $ git add b 
$ git co -m第二次提交
[master 16aa86a]第二次提交
1文件改变,0插入(+),0删除( - )
创建模式100644 b

现在我们来查看历史记录:

  $ git log --oneline 
16aa86a第二次提交
d263314第一次提交
38df137初始提交

正如你所看到的,有三个提交,哈希代码来填充它们。
有一个特殊的指针指向历史顶部的提交。它被称为 HEAD 。当提交 a 文件时,HEAD指向此提交,但当提交第二个文件时,HEAD开始指向第二个提交。



reflog 命令可帮助您跟踪HEAD指向的历史记录。 $ b

 < code $ git reflog 
16aa86a HEAD @ {0}:commit:第二次提交
d263314 HEAD @ {1}:commit:第一次提交
38df137 HEAD @ {2}:commit (初始):初始提交

为了摆脱最近的提交,您需要HEAD指向之前的一个提交。这是 reset 命令出现的时间。

  $ git reset  - 硬头@ {1} 
HEAD现在在d263314第一次提交

请小心,所有您的非分离数据会丢失。让我们来查看历史记录:

  $ git log --oneline 
d263314第一次提交
38df137初始提交

但是,不仅可以跳到之前的任何一个提交,也可以跳到任何一个提交我们回到第二次提交:

  $ git reset --hard 16aa86a 
HEAD is now at 16aa86a第二次提交

让我们查看历史记录:

  $ git log --oneline 
16aa86a第二次提交
d263314第一次提交
38df137初始提交



正如你所看到的,历史得到恢复



获得一些时间并阅读精彩的书我保证你会成为一个经验丰富的Git用户


First of all I'm new to web development in general and so let me know if I make any obvious mistakes.

So I decided that if I want to start making websites I'm probably going to need to learn Git and GitHub.

I currently understand how to initialize a repository, stage and commit and push it all to a GitHub page. What I'm struggling with now is the process of reverting back to an old commit. So lets say I've made two commits called commit 1, commit 2 respectively.

If I'm on commit 2 how to I go back to commit 1?? Can I go back to commit one change code commit the changes and still go back to commit 2 after?? What specifically happens to the code and files when moving between commits.

Please feel free to talk to me like a 5 year old who understands nothing to do with Git and the command line. Thanks in advance guys.

解决方案

You can use the reset command to navigate over the history. See how easy it is, at first let's create two files:

$ touch a
$ touch b

Let's commit one of them:

$ git add a
$ git co -m "First commit"
[master d263314] First commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a

Let's commit the other one:

$ git add b
$ git co -m "Second commit"
[master 16aa86a] Second commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b

Now let's check the history out:

$ git log --oneline
16aa86a Second commit
d263314 First commit
38df137 Initial commit

As you can see there are three commits and they have hash codes to distingush them. There is a special pointer that points at the commit that is on top of the history. It is called HEAD. When the a file was committed HEAD pointed at this commit, but when the second file was committed HEAD started to point at the second commit.

The reflog command helps you to track history where HEAD pointed.

$ git reflog
16aa86a HEAD@{0}: commit: Second commit
d263314 HEAD@{1}: commit: First commit
38df137 HEAD@{2}: commit (initial): Initial commit

In order to get rid of the most recent commit, you need to make HEAD point at one of the previous commits. This is when the reset command appears.

$ git reset --hard HEAD@{1}
HEAD is now at d263314 First commit

Be carefull, all your unstaged data gets lost. Let's check the history out:

$ git log --oneline
d263314 First commit
38df137 Initial commit

But it is possible to jump not only to one of previous commits, it is possible to jump at any commit as well, let's get back to the second commit:

$ git reset --hard 16aa86a
HEAD is now at 16aa86a Second commit

Let's check the history out:

$ git log --oneline
16aa86a Second commit
d263314 First commit
38df137 Initial commit

As you can see the history gets restored

Get some time and read this amazing book I guarantee you will become a highly expirienced Git user

这篇关于理解提交git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆