preface

When I was new to Git, I often encountered an awkward situation: git repository was often ruined by myself. Although I was not at the end of my teat, I would like to slap myself in the face

Of course, there are many ways to reset, and this article will give you a new way to solve the problem.

Method of use

Know the GIT DIFF

Git diff: git diff: git diff: git diff: git diff: git diff: git diff: git diff: git diff: git diff

What are detailed differences?

Every row of data changes in the workspace and staging area can be seen in this command, which is used to review code again before submitting it to avoid errors.

Git diff not only provides workspace and staging file comparison, but also provides any file difference, as follows:

  1. Git diff Filepath Compares the workspace with a file in the staging area
  2. Git diff HEAD Filepath Workspace is compared to HEAD(the current working branch), which can also be replaced with any commitID
  3. Git diff –staged or –cached Filepath staging compared to HEAD
  4. Git diff branchName filepath Compares files in the current branch with files in the branchName branch
  5. Git diff commitId Filepath is compared to a commit
  6. Git diff commitID1 commitID2 Compare files between two commit files

And then I thought, wouldn’t it be great to be able to recover files based on that information? Of course, I’m certainly not the first person to do this. Git already provides the corresponding command, and look below.

Generating a PATCH

Patch A patch is a text file generated based on git diff. The simplest method to generate a patch is git diff > test.patch.

Among them the test. The patch file records all of your changes this time, and if you forget about git Stash or force git reset –hard HEAD, this file can be a last resort.

GIT APPLY

Git apply Patch is used to add or delete content in the existing file according to the information in the patch file.

Add or delete operations here are not much different from manual changes, in that the changes remain in the workspace and are not committed to the staging area. After the operation, as you play.

The sample

We will generate a text file with the following contents and commit it:

origin context line1
origin context line2
Copy the code

Change it to:

origin context line1
origin context
add line
Copy the code

Git diff > test. Patch git diff > test. Patch git diff > test.

diff --git a/test b/test
index ce2f4b3..ae27ef5 100644
--- a/test
+++ b/test
@@ -1,2 +1,3 @@
 origin context line1
-origin context line2
+origin context
+add line
Copy the code

Then we use Git checkout. To reset the file to its original state and the text file to

origin context line1
origin context line2
Copy the code

Git apply test.patch, write the changes again, check the text file, and change back

origin context line1
origin context
add line
Copy the code

Git diff: Git diff: git diff: git diff: Git diff: Git diff: Git diff: Git diff

Git apply is a transactional command, reject all conflicting changes, and add –reject.

application

Git rebase is a much more powerful command than this one. This command is quite useless

Here are two vivid examples of the convenience of this command

Patch is used to download MR content

This method comes from my colleague in the department, who is an interesting person and often brings me all kinds of ideas, so I borrowed them from him. (In order to get this idea to be able to write, secretly slipped him 10 yuan to get authorization)

Those of you who use GitLab a lot know that when someone submits an MR to the master library, you can only review the code. You can’t directly download the changed file content to verify the code before the MR is merged.

This can be frustrating when there are too many changes to the file, or when it’s a front-end project and you really want to see how the code looks on the page.

Of course, for those of you who can imagine what the code looks like in the browser, never mind…

The key point here is how to obtain the patch of the comparison between the MR and the existing files in the master library. However, GitLab has already seen through all your little thoughts ~

Click on Email Patches to download Patches files, and then download the Patches to a local location via git Apply. Then run yarn Start or other commands to check the effect.

collaboration

Programmer’s work is known to all, weekends will occasionally work overtime to fix an urgent bug what, if you are on vacation sitting on a burning car, eating hot pot singing, a phone call, urgent bug, speed fix, really want a life ~

But at this time, the house leaks to meet even rain, VPN also can not connect to the company Intranet, what bad luck let you take up all, this how to do? !

Or call a colleague to help you change, 28 files need to change two or three lines of code, one by one…… Half a day passed

Or the whole modified file all sent in the past, poor signal transmission and slow, it is better to call to say, can be anxious I……

Git diff to make a patch and send it. The content of a bug modification is usually very small, less than 1KB…

The biggest advantage of patch is its small content. It can only record your changes instead of the whole file. Git Apply is not much better

supplement

Git format-patch commitA commitB and git am are two similar commands.

The former will also bring all the information of a commit, such as author, time, node description, etc., and then the latter will apply the patch content to the current branch. Unlike Git Apply, where the patch is no longer only in the workspace, you will find commit records between multiple commitA and commitB on the current branch. Specific details, please go to explore by yourself ~


Pay attention to wechat public number: KnownsecFED, code to get more quality dry goods!