官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 景泰县| 阆中市| 丘北县| 菏泽市| 托克逊县| 沈丘县| 贵州省| 城步| 出国| 双江| 海原县| 白沙| 耿马| 宣汉县| 高碑店市| 扎兰屯市| 临城县| 阜康市| 尼玛县| 玛曲县| 尼玛县| 淮南市| 巴马| 德兴市| 平泉县| 信阳市| 西乌珠穆沁旗| 奉化市| 万荣县| 荥经县| 大足县| 勃利县| 黑水县| 鸡东县| 咸阳市| 永济市| 隆安县| 锡林浩特市| 白朗县| 甘南县| 资兴市|