Home >Development Tools >git >How to use Git to commit partial modifications
Git is a distributed version control system that is very suitable for team collaboration or individual developers to manage the modification history of the code. In development, a situation often arises: some modifications need to be added to the submission instead of all submissions. In this case, you need to use the Git function to submit partial modifications.
There are two ways to commit partial modifications in Git: one is the interactive mode using the Git add command, and the other is using the Git stash command.
The Git add command is used to add modifications in the workspace to the staging area. Interactive mode allows us to select modifications to add rather than adding all modifications to the staging area at once. The usage is as follows:
git add -i
After executing the above command, you will enter interactive mode. The following are several commonly used commands:
Take adding a part of the modified file example.py
as an example, execute the following command:
git add -i example.py
Then select p, and then Git will display the modified differences , let us select the modifications to be added and the modifications not to be added. Once the selection is complete, use the q command to exit interactive mode. At this time, only the selected modifications are added to the staging area, and the remaining modifications remain in the workspace.
The Git stash command is used to save the current modifications, store all modifications in the workspace and temporary storage area, and restore the workspace to the last submitted state . This method can avoid errors when submitting partial modifications manually, and can also temporarily put existing modifications aside so that we can deal with other things.
The method of using the Git stash command is as follows:
git stash
After executing the above command, Git will store all modifications in the current workspace and staging area, and restore the workspace to the last time Submitted status. At this point, we can perform other operations without affecting the original modifications.
We can then pop (apply) the stored modifications using the following command:
git stash pop
This command applies the previously stored modifications to the workspace and removes them from the storage list. If you need to perform this operation multiple times, you can use git stash list to view previously stored modifications, and use git stash apply
In short, by using one of the above two methods, we can easily submit some modifications without having to submit all the modifications at once. This can greatly improve our work efficiency and avoid some wrong submissions.
The above is the detailed content of How to use Git to commit partial modifications. For more information, please follow other related articles on the PHP Chinese website!