- 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.
- Angular UI Development with PrimeNG
- Software Testing using Visual Studio 2012
- Learning Laravel 4 Application Development
- ADI DSP應用技術集錦
- PLC編程與調試技術(松下系列)
- HTML5 APP開發從入門到精通(微課精編版)
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- Scala編程(第5版)
- Android Sensor Programming By Example
- 快樂編程:青少年思維訓練
- 高質量程序設計指南:C++/C語言
- Dart:Scalable Application Development
- Mastering Machine Learning with scikit-learn
- Natural Language Processing with Python Cookbook
- Mastering Magento Theme Design