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.
--Knowing the basic usage of git hub (comit, push, pull, etc.)
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
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.
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.
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.
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.)
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.
②
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")
Create a new branch before reflecting it. Here, create a B branch from the master 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.
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")
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
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.
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.
-About merge conflicts (GitHub official)
-[Git] diff the stashed content
Recommended Posts