- Continuous Integration,Delivery,and Deployment
- Sander Rossel
- 525字
- 2021-07-02 15:42:16
The staging area
Once you commit your changes, they go out of the working directory and into your history. There is a stage between the two though, called the staging area. Whenever you change, add, or delete a file in your working directory and then check out the status using git status, it will tell you whether you have new (untracked) files, changes that are not staged for commit, or changes to be committed:

From that explanation, it becomes clear that whatever is not staged will not be committed. This allows you to change multiple files, but only commit a few of them. It is even possible to stage only parts of files. It can come in handy when you are working on some big changes and then someone asks you to fix the whatchamacallit. You can alter a couple of lines of code and stage and commit only those.
As we have seen before, putting your files into the staging area is easily done using the git add . command. Now, instead of using a dot (for all files), we can specify a single file. For example, git add repository.js.
We can stage and commit only parts of files that we changed. Such a part is called a hunk. First, create a new file, call it hunks.txt, and add four lines to it. You can just number the lines 1, 2, 3, and 4 and commit it using git add hunks.txt. Now add two lines, one between 1 and 2 and one between 3 and 4; just put in 1.1 and 3.1 or whatever you like. Your file should now look as follows:
1
1.1
2
3
3.1
4
Now you can create a patch, or some changes that you would like to stage. You can do this using the git add --patch filename command. You can now see the contents of your file and the hunks in green. You will be asked if you want to stage the current hunk and your options are [y,n,q,a,d,/,s,e,?]. Simply type ? and Enter to see what they mean:

In this case, the command line has both our files in one hunk, so we want to split the hunk by typing s. After that, you see only 1.1 is green and we do want to stage that, so we pick y. After that, we get two more hunks, but we only have one more added line. We do not want to stage 3.1, so pick n. The last hunk might be a Windows thing, I am not sure. Anyway, do not stage it:

Now when you use git status you will see that hunks.txt is both in the staged part and in the unstaged part.
One more thing I would like to say about git add is that there is an interactive commands option. This is probably the closest you get to an actual GUI in the console. You can get it using the git add -i command. Here, you can easily select files for staging, apply patches, remove files from staging, and see the changes you have made to files. You should check it out.
- 程序員面試白皮書
- Java范例大全
- 垃圾回收的算法與實現
- DevOps入門與實踐
- 跟老齊學Python:輕松入門
- Spring+Spring MVC+MyBatis整合開發實戰
- Mastering Linux Network Administration
- 單片機應用與調試項目教程(C語言版)
- Java Web程序設計任務教程
- 精通MATLAB(第3版)
- Learning Apache Cassandra
- 詳解MATLAB圖形繪制技術
- Mastering ArcGIS Enterprise Administration
- Arduino Wearable Projects
- 區塊鏈架構之美:從比特幣、以太坊、超級賬本看區塊鏈架構設計