Git notes (26) interactive temporary storage
1. Interactive temporary storage
Without IDE
Git comes with scripts that make it easier to work on the command line
There are several interactive commands that help you combine specific parts of a file into a commit
After modifying a set of documents, we hope that these changes can be put into several submissions
These tools can be very useful when you're not mixing them up to be a commit
In this way, you can ensure that the commit is a logically independent change set
It also makes it easy for other developers to audit when working with you
If you run git add with the - i or -- interactive option
Git will enter an interactive terminal mode and display something similar to 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: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now>
You can see that this command shows the staging area in a very different view
Basically the same information as git status, but more succinct
It lists the temporary changes on the left and the unstaging changes on the right
After this area is the command area
You can do some work here
- Include staging files
- Cancel temporary file
- Part of the staging file
- Add untracked files
- View differences in staging content
2. Temporary and cancel temporary files
If you type 2 or u at the what now > prompt, the script prompts you which file you want to save:
What now> 2 staged unstaged path 1: unchanged +0/-1 TODO 2: unchanged +1/-1 index.html 3: unchanged +5/-1 lib/simplegit.rb Update>>
To stage TODO and index.html files, enter a number:
Update>> 1,2 staged unstaged path * 1: unchanged +0/-1 TODO * 2: unchanged +1/-1 index.html 3: unchanged +5/-1 lib/simplegit.rb Update>>
The * in front of each file means that the selected file will be staged
If you don't enter anything at the update > > prompt and press enter directly
Git will stage the previously selected file:
Update>> updated 2 paths *** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> 1 staged unstaged path 1: +0/-1 nothing TODO 2: +1/-1 nothing index.html 3: unchanged +5/-1 lib/simplegit.rb
Now you can see that TODO and index.html files have been staged, while simplegit.rb files have not been staged
If you want to cancel staging TODO files at this time, use the 3 or r (undo) option:
*** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> 3 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
If you check the Git status again, you can see that the TODO file has been canceled
*** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> 1 staged unstaged path 1: unchanged +0/-1 TODO 2: +1/-1 nothing index.html 3: unchanged +5/-1 lib/simplegit.rb
If you want to see the differences between staged content, you can use the 6 or d (differences) command
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: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> 6 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 : support@github.com</div> +<div id="footer">contact : email.support@github.com</div> <script type="text/javascript">
With these basic commands, you can use interactive add mode to easily handle staging areas
3. Temporary patch
Git can also hold specific parts of a file
For example, if you make two changes in the simplegit.rb file
But it's OK to just hold one of them instead of the other
From the interactive prompt, type 5 or p (patch)
Git asks which files you want to partially stage, and then for each part of the selected file
It will show file differences one by one and ask if you want to 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,?]?
There are many options
Enter? To display 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 help
In general, you can enter y or n to choose whether to stage each block or not
Of course, it's also useful to stage all parts of a particular file or skip a chunk for later choices
If only a part of the file is staged, 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
The state of the simplegit.rb file is interesting
It shows how many rows are staged and how many rows are not staged
At this point, you can exit the interactive add script and run git commit to submit some temporary files
You can also save some files in interactive add mode
You can use git add -p or git add --patch on the command line to start the same script
Further, you can use the patch mode of reset --patch command to partially reset files
Check out -- patch command to partially check out files
Use the stash save --patch command to partially save files
Reference resources: git
The above contents are all composed of deletion, addition and modification according to git official website
Relevant recommendations:
Git notes (25) select revision
Git notes (24) maintenance items
Git notes (23) contributions from different roles
Git notes (22) key points of project contribution
Git notes (21) distributed workflow
Thank you