In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Git work area storage and how to solve the problems in branch management". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "Git work area storage and talk about how to solve the problems in branch management"!
Problem review
The problems encountered by the partners are as follows:
Now there is a master branch, and there is a file called 01.txt in the master branch, which contains only one line of data, then execute add and commit on the 01.txt, then create a new branch fa from the master branch, switch to the fa branch, and then add a row of data to the 01.txt. After the addition is successful, do nothing and switch back to the master branch. Use the cat command to view the 01.txt file. It is found that there are two rows of data. In theory, the 01.txt in master has only one row of data, while the 01.txt in fa has two rows of data. The whole process is shown below:
To understand this problem, you must first understand the following question:
The cat command has nothing to do with git, but is used to view files. I used the cat command for demonstration convenience, which is the same as opening a file directly with notepad.
Perhaps the sharp-eyed friend has noticed that we are missing two steps in the above operation, that is, the data in the add/commit,fa branch is modified and switched back to master instead of add/commit. Under normal circumstances (add/commit after data modification), if the data in the master and fa branches are inconsistent, we perform the git checkout-branch switch, and the contents of the files in the workspace will also change accordingly (you can view this change through the cat command or directly opening the files in the workspace in notepad), but if I modify the files in the fa branch without add/commit, switch back to master At this time, if the file in the workspace changes, it may cause my changes in the fa branch to be lost, so at this time, the file in the workspace remains unchanged, that is, the content of the file in the workspace is still modified in the fa branch.
To solve this problem, we have two solutions. Please read on.
Solution one
The first solution is to add and commit before switching branches after a certain branch modifies the file. This operation is relatively simple, and I won't demonstrate it here.
Option 2 (Storage)
The second solution is Stashing, which can be used in the following scenarios:
When I modify the file in one branch fa, but not completely, I don't want to add/commit at this time, but there is a more urgent thing I need to do on another branch fb. I have to switch branches.
In such a scenario, if I switch branches directly, there are two problems:
1. After switching from fa to fb, the workspace code is still fa code, which does not meet the requirements of my job.
two。 Suppose I don't care about question 1, modify the code in the workspace directly in fb, and then go back to fa after I modify it in fb. I will find that my previous code is missing.
To solve this problem, Git provides us with Stashing.
Now suppose that at the beginning, the contents of the files in the master and fa branches are consistent, and the workspaces of both branches are clean, that is, nothing needs add/commit. At this point, I modify the file in master, and after the modification is completed, execute the git status command. We see that there is something in master that needs add/commit. At this point, I want to switch to the fa branch, but I don't want to add/commit the master branch. At this point, we can execute the following command to store the files in the current branch:
$git stash
OK, execute the git stash command before executing git status. We find that the master branch is clean at this time. At this time, we can happily switch to the fa branch. After switching to the fa branch, we find that the changes in master do not interfere with the fa branch. After we have finished the work in the fa branch, we return to the master branch. Execute the following command to restore the data just stored:
$git stash apply
After the above command is executed, the files in the workspace in the master branch are restored, and when you execute git status, you can see that there is more data that needs add/commit.
We can also store the work area multiple times, at which time we can execute the following command to view the storage:
$git stash list
The execution results are as follows:
Git stash apply says to restore the most recent storage. If we want to restore to a previous storage, we can add the name of the storage, as follows:
$git stash apply stash@ {1}
There are other orders about storage:
1. Restore storage and stack $git stash pop
The execution effect is the same as that of git stash apply, except that after execution here, the storage at the top of the stack is removed.
two。 Delete a storage $git stash drop stash@ {4}
The last parameter refers to the name of the storage.
At this point, I believe you have a deeper understanding of "Git work area storage and talking about how to solve the problems in branch management". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.