[PYTHON] [Git] I tried to make it easier to understand how to use git stash using a concrete example

Introduction

When using Git Hub, do you have the following?

――I want to continue development using the contents of the pull request when I am pulling and getting confirmation from other people.

――I made a mistake in the branch I was working on, and I want to reflect the work I did in the branch I originally wanted to work on.

In such a case, the command ** stash ** is convenient, so I will introduce it with a usage example. I would appreciate it if you could point out if there is a more convenient method for git beginners.

As a premise

--Knowing the basic usage of git hub (comit, push, pull, etc.)

The environment I'm using

About stash

What is ** stash **? In a word, it is a command that can evacuate (hold) the change history from commit to ** stash list **.

I will explain using a concrete example.

Here is a rough procedure.

--Procedure ① Create A branch and test1.py pull request --Procedure (2) Add the contents to test1.py as A branch after pull request --Step ③ Evacuate the contents added to test1.py to git stash list --Procedure ④ ** Check the contents of stash with git stash list ** --Step ⑤ Create a new branch (Create B branch) --Procedure ⑥ Pull the contents of the remote to the B branch (the part to be pulled differs depending on the state of the A branch) --Procedure ⑦ Check the commands that can reflect the contents of git stash list --Procedure ⑧ Reflect the contents of git stash list

Step ① Create A branch and test1.py pull request

After creating the A branch from the master branch, I committed the following files and made a pull request.

test1.py


#First commit
print("test1")

I want to use the file I committed while waiting for the next pull request.

Step (2) Add the contents to test1.py as A branch after pull request

The following contents were added to test1.py while keeping the A branch.

test1.py


#First commit
print("test1")

#Add content
print("test2")

When you save, the changes in test1.py will be reflected in the unstaged part like the image.

スクリーンショット 2020-10-15 21 53 31

Next, I want to reflect this added content in a different branch. It is possible to commit as it is in the same branch, but basically it is not very pleasant to commit while the other party is checking it, so I would like to avoid it. This example was only added, but in reality, it is possible to notice the mistake in the part that was pulled in step 1 and correct it. (From the other party's point of view, if you look at it halfway, but you can change the content without permission, you'll have to look at it from the beginning.

Step ③ Evacuate the contents added to test1.py to git stash list

Evacuate the changes. Execute the git stash command in the terminal (command prompt for windows). Then the test1.py file in the A branch looks like this:

Terminal


git stash

test1.py


#First commit
print("test1")

It returns to the contents of the file when you committed in step ①. In other words, the content added in step 2 is evacuated to git stash list. There will be no changes to test1.py in the unstaged section. (It looks like the image below.)

スクリーンショット 2020-10-15 22 08 48

Step ④ Check the git stash list Check the command

Check the evacuated git stash list. The command to use is:

git stash list

(Content) You can see the git stash list. (Example of use)

Terminal


git stash list

<Execution result below>
stash@{0}: WIP on branch_name: commit_id2 commit_comment2

The above stash @ {0} contains the contents that were evacuated earlier. This 0 indicates that it is the 0th in the stash list. If there are more than one, specify the stash list you want to use with this number. If you run it and nothing happens, it means that you don't have a git stash list.

  • If you execute the git stash list command when satsh @ {0} is in the git stash list, stash @ {0} before execution becomes stash @ {1}. The newly stashed content becomes stash @ {0}. The smaller the number in {}, the newer the contents of the stash list.

git stash show stash@{0} -p

(Content) You can check the saved contents of git stash list. 0 in stash @ {0} specifies which stash list you want to check when there are multiple git stash lists. The -p part can be replaced with --patch.

(Example of use)

Terminal


git stash show stash@{0} -p

<Execution result below>
#First commit
print("test1")
+
+#Add content
+print("test2")

git diff stash@{0}

(Content) You can display the diff of git stash list. The explanation in words is difficult to understand, so please see an example of use.

(Example of use)

Terminal


git diff stash@{0}

<Execution result below>
#First commit
print("test1")
-
-#Add content
-print("test2")

Step ⑤ Create a new branch (Create B branch)

Create a new branch before reflecting it. Here, create a B branch from the master branch.

Step ⑥ Pull the contents of the remote to the B branch (the part to be pulled differs depending on the state of the A branch)

After creating the B branch, I pull the contents of the remote, but the pulling part changes depending on whether the pull request of the A branch is merged or not.

  1. If merged

Use git pull origin master to reflect the contents of the remote master branch in the B branch. The contents of test1.py at that time are as follows.

test1.py


#First commit
print("test1")
  1. If not merged Reflect the contents of the A branch in the pull request by git pull origin A. You can still reflect the contents of the A branch of test1.py in the B branch. The reflected content is the same as above.

test1.py


#First commit
print("test1")

※Note

If you modify the ** pulled part ** by the method 2, you will get a conflict if you try to merge the pull request of the B branch after the pull request of the A branch is merged. Even if there is a conflict, there is no problem as long as it is resolved each time. Conflicts will not occur if the pulled part is not corrected and only added.

(Supplement) When you want to pull the contents of A branch to B branch, you may have multiple commits in A branch and want to reflect the contents of one of them. At that time, there is a convenient command called git cherry-pick. If you want to know about cherry-pick, there is a person who explains it in an easy-to-understand manner at the following URL, so please refer to that.

[git] Summary of how to bring work from other branches to your branch

Step ⑦ Check the commands that can reflect the contents of git stash list

There are two main commands that can be reflected.

○ First

git stash apply stash@{0}

After reflecting this, the git stash list will remain as it is.

○ Second

git stash pop stash@{0}

This will remove the git stash list after it has been reflected.

The number in {} is an integer to specify which stash list to reflect. If you do not specify stash list, stash @ {0} is specified by default.

Step ⑧ Reflect the contents of git stash list

The contents evacuated by the git stash pop command confirmed in step ⑦ are reflected as follows.

Terminal


git stash pop stash@{0}

Post-execution test1.py file

test1.py


#First commit
print("test1")

#Add content
print("test2")

With this, the changes made after the pull request can be reflected in the B branch in step (1). If you run git stash list after this, there is no stash @ {0}. When executed by git stash apply stash @ {0}, stash @ {0} remains.

Reference url

-About merge conflicts (GitHub official)

-[Git] diff the stashed content

Recommended Posts

[Git] I tried to make it easier to understand how to use git stash using a concrete example
I tried to make a ○ ✕ game using TensorFlow
A Python beginner made a chat bot, so I tried to summarize how to make it.
I tried to make a stopwatch using tkinter in python
I tried to make a simple text editor using PyQt
I tried using a library (common thread) that makes Python's threading package easier to use
I tried to make a regular expression of "amount" using Python
I tried to make a regular expression of "time" using Python
I tried to make a regular expression of "date" using Python
I tried to make a todo application using bottle with python
I tried to make a Web API
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
[Python] Explains how to use the range function with a concrete example
I tried to make a calculator with Tkinter so I will write it
A simple example of how to use ArgumentParser
I read "How to make a hacking lab"
I tried to use Java with Termux using Termux Arch but it didn't work
When I tried to make a VPC with AWS CDK but couldn't make it
I tried to make a translation BOT that works on Discord using googletrans
I tried to make a suspicious person MAP quickly using Geolonia address data
I tried to make a "fucking big literary converter"
I tried to summarize how to use matplotlib of python
How to make a Python package using VS Code
I tried to summarize how to use pandas in python
How to use Decorator in Django and how to make it
I tried to draw a configuration diagram using Diagrams
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
Understand how to use django-filter
I tried to make a document search slack command using Kendra announced at re: Invent 2019.
I tried "How to get a method decorated in Python"
I tried using git inspector
I tried to automate [a certain task] using Raspberry Pi
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
I tried to summarize how to use the EPEL repository again
I tried to make a site that makes it easy to see the update information of Azure
[Python] I tried to make a simple program that works on the command line using argparse.
[5th] I tried to make a certain authenticator-like tool with python
I tried to get a database of horse racing using Pandas
[2nd] I tried to make a certain authenticator-like tool with python
I wrote Django commands to make it easier to debug Celery tasks
A memorandum when I tried to get it automatically with selenium
[Python] I tried to implement stable sorting, so make a note
I tried to implement anomaly detection using a hidden Markov model
[3rd] I tried to make a certain authenticator-like tool with python
I tried to make a periodical process with Selenium and Python
I tried to get a list of AMI Names using Boto3
I tried to make a 2channel post notification application with Python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to make a strange quote for Jojo with LSTM
I tried to make a mechanism of exclusive control with Go
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make a Blender plugin
How to make a crawler --Basic
I tried to verify how fast the mnist of Chainer example can be speeded up using cython
I tried to make a system to automatically acquire the program guide → register it in the calendar in one day
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
Python: I tried to make a flat / flat_map just right with a generator