Why do we need it

I have to say, both when I learned about this command and when I used it afterwards, I loved this command because it worked so well.

To give you a scenario where I use this command:

At this point I was working on feature_666, working on a feature_666 module, writing code like a keyboard. Then the customer reported a bug that was very serious and needed to be fixed immediately, priority 0!! So, I need to go to the release branch to checkout to work on the new branch, but 666 is not finished yet. At this point, I was faced with A choice: A) commit and switch, saving the code to the branch feature_666, which resulted in A meaningless commit; B) don’t commit and switch, which no one chose at all.

Is it hard to choose? At this point, don’t forget that there is option C!

C: With git Stash, store current changes (uncommitted code) in the cache, switch branches to fix bugs, and come back to retrieve them via git Stash pop.

Little example, let’s try it

Store modification

OK, at the last commit, the code snapshot looked like this

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String s = "Hello, I'm Han "; }}Copy the code

At this point, I am writing the following code:

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String s = "Hello, I'm Han "; String s1 = "I'm writing a super cool feature right now, but I'm not finished yet, and I'm even reporting some errors "; }}Copy the code

At this point in the code, the emergency bug appears, cannot wait a second, select the following action

Git checkout <bug_branch>Copy the code

The cached workspace code reverts to the code from the last submission:

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String s = "Hello, I'm Han "; }}Copy the code

To view changes

If you’ve ever lost code before, you might be worried about a new command you haven’t touched before, so how can you be sure it’s working?

Git stash showCopy the code

Take out the modification

Now that the bug has been fixed, it is time to come back to development and take out the modifications

Git checkout <feture_branch> git stash popCopy the code

Take out the modified workspace code as:

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String s = "Hello, I'm Han "; String s1 = "I'm writing a super cool feature right now, but I'm not finished yet, and I'm even reporting some errors "; }}Copy the code

More details about it

I’m sure you already know how to use git Stash from the examples above, but if you understand the ins and outs of it, you’ll be more flexible and comfortable with it.

Where are the changes stored?

When you use git init to add version control to a project, a.git hidden folder is generated in the project path. .git stores all version management information. .git/refs/stash stores the node pointer corresponding to the last stash

Also, you can see all of our stash records in.git/log/refs/stash

Multiple stash cases

Ok, let's try to modify the file and then use git Stash again. Now we have two temporary changes, so how do we view them?Copy the code
Git stash list // View all the staging changes in the staging areaCopy the code

Have you noticed that these two names are the same? What the hell is this? Don’t worry, the name is the same, but the pointing changes are different. As we can see from.git/log/refs/ Stash, the corresponding node Pointers are different.

For a puzzle with the same name, read on

How do you get the name of Stash storage? Can you change it?

When you create a Stash with git Stash, you give it a default name.

As we said earlier, stash stores all the changes in the current workspace since the last commit of the current branch. So stash’s default naming rule is:

WIP on <branch_name> : <latest_commit_id> <latest_commit_message>Copy the code

WIP, short for Work In Progess, stands for workspace progress. The same goes for Index, which represents progress that has been added but has not yet been committed.

If you execute git stash twice without committing it, as shown in the picture above, you can’t tell exactly what the two stash changes are. .

So, in this case, it’s obviously important to give the Stash store modification a name, as follows:

git stash save <message>Copy the code

There are several ways to get it out

In all of the examples above, the way to take stash out is

Git stash pop // Get the most recent stash and delete the record from the record listCopy the code

This is a very useful method of removal and is used most often, but not in all cases.

Because git Stash pop is the stash that pops out of the top of the stack, the last stash that’s stored. This doesn’t work if you have multiple stash stash cases where you want to take out the one that’s not at the top of the stack.

Use:

Modify git stash apply stash@{X} // Remove the corresponding stash from the record listCopy the code

conclusion

Although all git commands can be viewed from git documentation, it is always necessary to type these commands yourself to master these skills.

I have to say, using the command line is really the right way to use Git.

Finally, good night to the world.