This article is shared by Huawei cloud community “Git you may not know interactive temporary storage”, by Long Ge’s Notes.
Several interactive Git commands in this section can help you combine specific parts of a file into a commit. These tools can be very useful when you are making changes to a large number of files and want the changes to be split into several commits rather than lumped together into one. This way, you ensure that the commit is a logically independent change set, while also making it easy for other developers to review while working with you. If you run Git add with the -i or –interactive options, Git will enter an interactive terminal mode that displays something like the following:
$ git add -i
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now>
Copy the code
You can see that this command displays the staging area in a very different view than usual — basically the same information as Git status, but a little more concise. It lists provisioned changes on the left and unprovisioned changes on the right.
Following this area is the “Commands” command area. Here you can do a few things, including temporary file, untemporary file, temporary part of the file, add files that are not tracked, and show the difference between the contents of the temporary file.
Temporary and untemporary files
If you type u or 2 (update) after the What Now > prompt, it will ask you which file you want to temporarily store:
What now> u
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
Update>>
Copy the code
To temporarily store TODO and index.html files, enter a number:
Update>> 1,2 Unstaged path * 1: Unchanged +0/-1 TODO * 2: Unchanged +1/-1 unchanged +5/-1 lib/simplegit.rb Update>>Copy the code
The * before each file means that the selected file will be temporarily saved. If you type nothing after the Update>> prompt and press Enter directly, Git will temporarily save the previously selected file:
Update>>
updated 2 paths
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> s
staged unstaged path
1: +0/-1 nothing TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rb
Copy the code
Now you can see that the TODO and index.html files have been provisioned but the simplegit.rb file has not been provisioned. If you want to cancel the temporary TODO file at this point, use the R or 3 (undo) option:
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> r
staged unstaged path
1: +0/-1 nothing TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rb
Revert>> 1
staged unstaged path
* 1: +0/-1 nothing TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rb
Revert>> [enter]
reverted one path
Copy the code
The TODO file has been cancelled:
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> s
staged unstaged path
1: unchanged +0/-1 TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rb
Copy the code
If you want to see the difference between the provisioned content, you can use the D or 6 (difference) commands. It displays a list of staging files from which you can select the staging differences you want to view. This is very similar to specifying git diff –cached on the command line:
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> d
staged unstaged path
1: +1/-1 nothing index.html
Review diff>> 1
diff --git a/index.html b/index.html
index 4d07108..4335f49 100644
--- a/index.html
+++ b/index.html
@@ -16,7 +16,7 @@ Date Finder
<p id="out">...</p>
-<div id="footer">contact : [email protected]</div>
+<div id="footer">contact : [email protected]</div>
<script type="text/javascript">
Copy the code
With these basic commands, the staging area can be easily processed using interactive add mode.
Temporary patch
Git can also temporarily store specific parts of a file. For example, if you made two changes in the simplegit.rb file, but you only want to temporarily save one and not the other, Git will do it for you easily. At the same interactive prompt as in the previous section, enter P or 5 (patch). Git will ask you which files you want to partially store. Then, for each part of the selected file, it displays the file distinctions one by one and asks you if you want to temporarily save them:
diff --git a/lib/simplegit.rb b/lib/simplegit.rb index dd5ecc4.. 57399e0 100644 -- a/lib/simplegit.rb +++ b/lib/simplegit.rb @@-22,7 +22,7 @@class simplegit end def log(treeish = 'master') - command("git log -n 25 #{treeish}") + command("git log -n 30 #{treeish}") end def blame(path) Stage this hunk [y,n,a,d,/,j,J,g,e,?] ?Copy the code
There are many options. Input? Displays a list of all available commands:
Stage this hunk [y,n,a,d,/,j,J,g,e,?] ? ? y - stage this hunk n - do not stage this hunk a - stage this and all the remaining hunks in the file d - do not stage this hunk nor any of the remaining hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print helpCopy the code
Typically, you can type Y or N to select whether you want to temporarily store each block, although it is also useful to temporarily store all parts of a particular file or skip a block for later selections. If you only temporary part of the file, the status output might look like this:
What now> 1
staged unstaged path
1: unchanged +0/-1 TODO
2: +1/-1 nothing index.html
3: +1/-1 +4/-0 lib/simplegit.rb
Copy the code
The state of the simplegit.rb file is interesting. It shows how many rows are staged and how many rows are not. This file has been partially provisioned. At this point, you can exit the interactive add script and run Git Commit to commit some of the temporary files.
You can also avoid doing partial file staging in interactive add mode — you can start the same script from the command line using git add -p or git add –patch.
Further, you can partially reset files using the git reset –patch mode command, partially checkout files using the git checkout –patch command, and partially hold files using the git stash save –patch command. We’ll learn more about these commands in more detail when we look at advanced uses of them.
Click to follow, the first time to learn about Huawei cloud fresh technology ~