Undo changes four stages of Git, it is necessary to understand

3 steps

Under normal circumstances, our job is three steps

git add . 
git commit -m 'comment'
git push

  

Description:

1, git add all the files into a temporary storage area;

2, git commit all the documents submitted to the local repository from scratch;

3, git push all the submissions from the local warehouse to the remote repository.

4 area

The reason git puzzling, mainly because it compared to the traditional version, etc. svn management tools, multi introduces the concept of a temporary storage area (Stage), it is because this concept more, leaving many people wondering. In fact, beginners, how each specific area of ​​work, we totally do not care, but just know that there are so four is enough area:

1. Work area (Working Area)

2. temporary storage area (Stage)

3. Local warehouses (Local Repository)

4. remote repository (Remote Repository)

5 states

More than four zones, will have to enter a state after the success of each district, plus a very initial state, a total of five states. We put the following five states were named as:

1. unmodified (Origin)

2. Modified (Modified)

3. have been staging (Staged)

4. submitted (Committed)

5. Pushed (Pushed)

Check for Modifications

After understanding the basic concepts, we talk about how to make mistakes after the revocation of the problem. First of all, we need to understand how these three steps to check them every step of what has changed, and we go there a judge modified successfully. Check modified two identical commands are the diff, but different parameters.

Modified, non-scratch

git diff

  First, let's look, if we simply look at the file saved in the browser, but have not done git add. Before, we have to check how what changes. Let's just take a look at the file to do the experiment

We are the second line of the beginning of the file casually added a 4 digit 1234, save the file, then the file has been modified into the state, but have not yet entered the staging area, we run git diff, as follows:

$ git diff
diff --git a/app/Http/Controllers/Admin/VideoController.php b/app/Http/Controllers/Admin/VideoController.php
index 5a3a49d..757c775 100644--- a/app/Http/Controllers/Admin/VideoController.php+++ b/app/Http/Controllers/Admin/VideoController.php@@ -3,7 +3,7 @@
  * Created by PhpStorm.
  * User: cyw
  * Date: 2019/6/7- * Time: 12:11+ * Time: 12:111234
  */

 namespace App\Http\Controllers\Admin;

  The results git diff tells us which files have changed what.

It has been staging, uncommitted

git diff --cached

  Now we modify into a staging area to look at. First execution git add., And then do git diff, you will not find any results.

This shows that the git diff command to check only difference between our work area and staging area, if we want to see the difference between the staging area and the local repository, you need to add a parameter git diff --cached:

diff --git a/index.md b/index.mdindex 73ff1ba..1066758 100644--- a/index.md+++ b/index.md<a href='http://www.jobbole.com/members/li754132448'>@@</a> -1,5 +1,5 <a href='http://www.jobbole.com/members/li754132448'>@@</a>
 ---
-layout: main
+1234layout: main color: black
 ---

  This time we see the difference is the difference between the staging area and local warehouses.

Has been submitted, did not push

git diff master origin/master

  Now, we submit the modification to a local warehouse from scratch, let's look at the differences. First implementation of git commit, then execute git diff --cached, there is no difference, execute git diff master origin / master, you can see the difference:

Here, master is your local repository, and origin / master is your remote repository, master is the main branch of meaning, because we are working on the main branch, so both sides here are master, and on behalf of remote origin.

Undoing Changes

After a clear understanding of how to check various modifications, we began to try a variety of undo.

Modified, non-scratch

If we just modify the file in the editor, but not yet implemented git add, this time we are still working document area, and did not enter the staging area, we can use:

git checkout .

  or

git reset --hard

  To undo.

You can see, after you execute git checkout., Modification has been revoked, git diff nothing up.

One pair of antonyms git add. Is the opposite of git checkout .. After doing changes, if you want to go a step forward for the changes into the staging area, on the implementation of git add., If you want to take a step back, just undo changes, implementation git checkout.

  

It has been staging, uncommitted

You have to perform a git add., But not yet implemented git commit -m "comment". This time you realize your mistake and want to undo, you can perform:

git reset
git checkout .

  or

git reset --hard

  git reset just returned for revision to the git add. before the state, that is still in the file itself is not modified buffer status, if you want to return an unmodified state, also need to perform git checkout ..

Perhaps you have noticed, the above two steps can use the same command git reset --hard to complete. Yes, it is this powerful command, you can step to put your changes completely restored to an unmodified state.

Has been submitted, did not push

Your hand too soon, you can either execute the git add., And executed git commit, this time you have entered the code to your local repository, but you regret it, how to do? Do not worry, there are ways.

git reset --hard origin/master

  Or the git reset --hard command, but this one more parameter origin / master, as we have said above, origin / master on behalf of a remote repository, since you have polluted your local repository, then from a remote repository Come to retrieve the code.

Pushed

Unfortunately, your hand is too fast, you only git add up, and the git commit, and also git push, and then you have entered the remote code repository. If you want to restore it, but fortunately, thanks to your local repository and remote warehouses are equivalent, you only need to first restore the local repository, and then forced to push to a remote repository like:

git reset --hard HEAD^
git push -f

  

to sum up

Undo We have used the same command git reset --hard more than four states, usage before two states even exactly the same, so long as the master git reset --hard usage of this command, since you never have to worry submitted mistake.

 

Guess you like

Origin www.cnblogs.com/ymdphp/p/11127117.html