Home > Development Tools > git > body text

How to use Git to commit partial modifications

PHPz
Release: 2023-04-03 10:29:25
Original
1616 people have browsed it

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.

Interactive mode using Git add 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
Copy after login

After executing the above command, you will enter interactive mode. The following are several commonly used commands:

  • p: add file modifications patch by patch;
  • s: add file modifications patch by patch and will be different from the staging area Put the modifications into the temporary storage area;
  • q: Exit interactive mode.

Take adding a part of the modified file example.py as an example, execute the following command:

git add -i example.py
Copy after login

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.

Use Git stash command

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
Copy after login

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
Copy after login

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 to apply the specified modifications.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template