- 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.
- Vue 3移動Web開發與性能調優實戰
- 從零構建知識圖譜:技術、方法與案例
- 假如C語言是我發明的:講給孩子聽的大師編程課
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- Java Web程序設計任務教程
- 小程序,巧應用:微信小程序開發實戰(第2版)
- QGIS Python Programming Cookbook(Second Edition)
- 零基礎PHP從入門到精通
- PHP 7 Programming Blueprints
- Java核心技術速學版(第3版)
- Puppet Cookbook(Third Edition)
- Roslyn Cookbook
- Java Web開發任務教程
- Python自動化開發實戰
- Learning Ext JS(Fourth Edition)