Git export stash backup as patch file

Git stash push backs up the current changes

When we use git for code management, a good advantage is that we can use git command to temporarily back up our modified files and restore them in time

When we need to temporarily insert another function to modify a function, we need to back up the previous modification, restore the local code and modify the new function. At this time, we can use git stash command to seamlessly implement it

The GIT stage push (which can be abbreviated as git stage) command can back up our current working area (CACHE) and cache area (index or stage). When the backup is successful, the changes to the working area and cache area will be cleared (without relevant parameters by default) When we need to restore, use git stash apply or git stash pop to restore
For more parameters of the stash push/pop/apply command, refer to Official documents.

Example

When test After the SH file is modified, we can see the changed file in the workspace

➜  stash-git git:(master) ✗ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test.sh

When executing git stash for backup

➜  stash-git git:(master) ✗ git stash push
Saved working directory and index state WIP on master: 022d738 init
➜  stash-git git:(master) git status
On branch master
nothing to commit, working tree clean

Restore when git stash pop is executed

➜  stash-git git:(master) git stash pop
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   test.sh

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (7ca79cc65abdb6c2224f5b0f95620e2c50cc420c)

Import the backup of Git stash as a patch file

Our commonly used git stash command is generally used for backup and restore of local computer warehouse files. How to restore cross computer backup? Perhaps the most commonly used is to use patch package for backup and restore

In git, you can use git diff > XXX The patch command generates the patch difference package of the text file. You can also use git diff -- binary > XXX The patch command generates a patch difference package for binary files However, it is frustrating that diff command can not generate difference package for modified text file and binary file at the same time

To meet this requirement, we have a better way to export git stash as patch package, because stash can support both text files and binary files

First, we use the GIT stash command to back up our local modifications, and then we can use git stash show "stash @{0}" - P > changes1 The patch command selects the backup of a git stash as a patch file (Note: {0} indicates the most recent backup)

Example

When using git stash to back up local modifications, you can use git stash list to view the backup list

➜  stash-git git:(master) git stash list
stash@{0}: WIP on master: 022d738 init

Use git show to generate a patch file and view the generated patch file

➜  stash-git git:(master) git stash show "stash@{0}" -p > changes1.patch
➜  stash-git git:(master) ✗ cat changes1.patch
diff --git a/test.sh b/test.sh
index 1dd57dc..6babd0c 100644
--- a/test.sh
+++ b/test.sh
@@ -1,3 +1,5 @@
...

Git apply patch file

In git, we use git apply XXX Patch applies the patch file. Several common parameters of the apply command need to be known as follows:

-p                 Specifies the relative depth of the file path in the patch, If we fight patch It is carried out in the directory at the upper level of the project, 
                   In the project directory, you can use `-p1` Apply patches.( Default to-p0 ).

--3way             If the merge fails, Adopt 3 way The algorithm is merged.

--reject           If the above method fails to merge, It can be solved by itself .rej File conflict.

--whitespace=fix   Ignore spaces.

For example, use the -- reject merge command:

git apply --reject --whitespace=fix -p0 patches/rtc/rtc_patch_all_lib.patch

Keywords: git

Added by kendall on Wed, 19 Jan 2022 01:48:05 +0200