Hello everyone, today I would like to share some cool git statements that not everyone knows and take advantage of. I would like to put away the original theory about GIT and go straight to the main point of this article.
1. Git stash
Save the changes
The git stash
command is used when you want to save the changes that you have not commit
, which is very useful if you are bad at executing the code
at the current branch
and want to checkout
over another branch
.
To save all the code in progress, you can use the following command:
1 2 | git stash save <span class="token string">"save 1"</span> |
or
1 2 | git stash save <span class="token string">"save 2"</span> <span class="token operator">-</span> u |
The difference between the two git stash save
commands above is that: the first stash save
will only save files that already exist, if adding option -u
will save new files that never existed in the repo.
You can git stash
as many times as you like and each time git will save the entire change as an element in a stack
.
Redeem change
To view the list of stash
you are saving we use the command:
1 2 | git stash list |
If you want to see the content of the changes, use the option -p
:
1 2 | git stash list <span class="token operator">-</span> p |
Or view the specific content of a specific change:
1 2 | git stash show <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> <span class="token punctuation">{</span> index <span class="token punctuation">}</span> |
When you want to apply again changes from certain stash:
1 2 | git stash apply <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> <span class="token punctuation">{</span> index <span class="token punctuation">}</span> |
or
1 2 | git stash pop index |
Delete unnecessary changes
When you want to undo changes and delete changes, the content is stored in the stack
:
1 2 3 | git stash apply <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> {index} git stash drop <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> {index} |
or
1 2 | git stash pop <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> {index} |
Or when you want to delete all saved changes:
1 2 | git stash clear |
Note : index
is the location of stash
. Demo of the results:
2. Git cherry-pick
So when is the git cherry-pick
question used?
When you want to bring changes from commit
on another branch
current branch
. The syntax is as follows:
1 2 | git cherry-pick id_commit |
If cherry-pick commit
conflict
occurs, you just need to fix the conflict
and add it to the commit
is okay. And if we want to cherry-pick
multiple commit
we just need to add the following:
1 2 | git cherry-pick id_commit_1 id_commit_2 id_commit_n |
After the cherry-pick
completed, push the code up as usual.
3. Git rebase -i HEAD ~ n
If the project you are working on makes a single pull request
only one commit
while you push
multiple commit
, this command will help you do that.
First try to log to see how many commits you currently have:
1 2 | git log --oneline |
After observing how many commits we now take this important step:
1 2 | git rebase -i HEAD~n // n là số commit mà chúng ta biết được thông qua câu lệnh trên nhé |
Then it will display the screen as below:
We will press the insert key, move to the commit to merge, fix the pick to fixup, then we press esc , enter: wq to save the changes. Then continue to push up is okay. Achievements after aggregation:
Above are my share, hope to help you. If anyone has any more interesting command, please comment below to share your knowledge. Thank you.