1, Foreword
- Understand most of the daily commands and workflow required to manage or maintain Git warehouse and realize code control, try to follow the basic operations of tracking and submitting files, and master the power of staging area and lightweight branching and merging. If you want to further study Git, you can learn some more powerful functions of Git. These functions may not be used in daily operation, but they may still play a key role in some times.
- If you are not clear about Git's basic use process, branch management, managed server technology, distributed workflow and other related technologies and capabilities, please refer to the blog:
-
-
-
-
-
- After modifying a large number of files, if you want these changes to be split into several submissions rather than mixed into one submission, you can combine specific parts of the file into a submission through Git command. This way can ensure that the submission is a logically independent change set, and it will also make it easy for other developers to review when working with us. If you use the - i or -- interactive option when running git add, 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: [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>
- You can see that this command displays the temporary storage area in a very different view than usual. It basically has the same information as git status, but it is more concise. It lists the temporary modifications on the left and the non temporary modifications on the right. After this area is the "Commands" command area, which can do some work, including temporary files, canceling temporary files, part of temporary files, adding untraceable files, and displaying the differences of temporary contents.
2, Staging and UN staging files
- If you type u or 2 (update) at the what now > prompt, it asks us which file we want to stage:
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>>
- To temporarily store TODO and index.html files, you can 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 temporarily saved. If you do not enter anything at 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
- Now you can see that the TODO and index.html files have been temporarily stored, but the simplegit.rb file has not been temporarily stored. If you want to cancel the temporary storage of TODO files, 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
- Viewing the Git status again, you can see that the temporary 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
- If you want to view the differences between the staged contents, you can use the d or 6 (difference) command. It will display a list of staged files, from which you can select the staging differences you want to view, which 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 : support@github.com</div>
+<div id="footer">contact : email.support@github.com</div>
<script type="text/javascript">
- These basic commands make it easy to work with staging areas using interactive add mode.
3, Temporary patch
- Git can also stage specific parts of a file. For example, if you make two changes in the simplegit.rb file, but only want to temporarily store one of them instead of the other, GIT will help us easily. Git will ask which files we want to partially stage; Then, for each part of the selected file, it will display the file differences one by one and ask whether you want to temporarily store 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,?]?
- At this time, there are many options. Enter? 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 help
- Usually, you can enter y or n to choose whether to stage each block. Of course, it is also very useful to stage all parts of a specific file or skip a block for subsequent selection. If only a part of the file is staged, the status output may look like the following:
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 that several lines have been staged and several lines have not been staged. The file has been partially staged. At this point, you can exit the interactive add script and run git commit to commit some of the staging files.
- You can also use git add -p or git add --patch on the command line to start the same script instead of staging some files in the interactive add mode.
- Further, you can use the patch mode of git reset --patch command to partially reset the file, use git checkout --patch command to partially check out the file and git stash save --patch command to partially temporarily store the file.