Home > Development Tools > git > body text

git temporary code change

PHPz
Release: 2023-05-17 09:47:07
Original
571 people have browsed it

In the software development process, we often need to modify the code to meet project needs. In a version control system, such as Git, we can easily create and save each version of the code to backtrack and restore the code status at any time. But sometimes, we need to make temporary changes to a version of the code, and these changes are not worth committing to the repository. At this time, we need to master some Git skills to temporarily change the code.

1. Use the stash command

The stash command can save the currently modified code to a temporary storage area so that we can restore it to this temporary storage area at any time when we perform other operations. We can save the current modifications to stash through the following command:

$ git stash
Copy after login

When we need to restore this code modification, we can use the following command:

$ git stash apply
Copy after login

This command will save the latest stash To restore to the current code, if there are multiple stashes, you can use stash@{x} to specify them.

If we need to save the modifications to the current staging area and the workspace at the same time, we can use the following command:

$ git stash save 'message'
Copy after login

Among them, message is the comment of the saved stash.

2. Use the reset command

The reset command can clear the temporary storage area or restore a file to a certain version. We can use the following command to restore a file to the state of the previous submitted version:

$ git reset HEAD <file>
Copy after login

If you need to restore the entire workspace to the state of the previous submitted version, you can use the following command:

$ git reset --hard HEAD
Copy after login

This will clear all modifications in the staging area and work area.

If you need to restore to a specified version, you can use the following command:

$ git reset --hard <commit>
Copy after login

This will restore the workspace to the specified commit version state.

It should be noted that the reset command will forcefully clear the contents of the temporary storage area and work area, so use it with special caution.

3. Use the checkout command

The checkout command can restore the modification of a certain file to the specified version state, but it will not delete the staging area and submission history. Here's how to use the checkout command to restore a file to the state of the last submitted version:

$ git checkout -- <file>
Copy after login

This will restore the file to the state of the previous submitted version, that is, without modifications.

If you need to restore to a specified version, you can use the following command:

$ git checkout <commit> -- <file>
Copy after login

This will restore the file to the specified commit version status.

It should be noted that the checkout command directly overwrites the contents of the current file, so use it with special caution.

4. Using branches

We can also use Git's branch mechanism to make temporary modifications to the code without affecting the code of the main branch. We need to create a new branch in the current state of the code, and then make modifications on this branch. As shown below, we checkout a new branch from the current branch, and then make modifications on this branch:

$ git checkout -b new_brach
... // 在新分支上进行修改
Copy after login

If you need to return to the code of the main branch, you can use the following command:

$ git checkout master
Copy after login

This will switch us back to the main branch, while the code on the new branch will remain unchanged.

When we complete the modification to the code, we can merge the new branch to the main branch in order to retain the code modification:

$ git checkout master
$ git merge new_branch
Copy after login

In this way, our code modification is successfully saved .

Summary

Git provides many ways to save temporary code changes. Use the stash command to quickly save the current modifications, use the reset command to clear the staging area and workspace or restore to a certain version state, use the checkout command to restore the modifications of a single file, and use branches to make changes without affecting the main branch. , modify and save the code. We need to choose the most appropriate way to temporarily change the code according to the actual situation in order to better manage our project code.

The above is the detailed content of git temporary code change. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!