An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
Mastering on 20 git stash commands
Hey Dev friends, It's a long time to see you. Anyways, let's get into the topic. You probably face the situation be like, while seriously working on some feature or a bug fix and need to switch other branch for inevitable reasons. If we tried to switch the branch, which results
- The changes come with us while switch the branch
--------------------- or ---------------------------
- Git doesn't allow to switch due to some conflicts
At the same time, you can't commit half-done work.
According to Golden rule of version control, Don't commit half-done work
Just stash the changes
Use anyone of the below commands to stash the staged and unstaged changes in stash stack. It's undo to latest commit and not delete the changes which stored in stash stack.
git stash
or
git stash save
Stash the untracked files
Want to stash the untracked files. Just use -u
flag on end of the command
git stash -u
or use --include-untracked
on end of the command
git stash --include-untracked
List the stashes
List out all the stashes which stored as latest stash will be top of stack, old stashes will be bottom of the stack.
git stash list
which will list the stashes like below format,
Hope you didn't get what it means, It's completely fine. Let's reveal the unknown terms on stash listing..
Stash@{0} - It's a stash reference. Which means refer the particular stash. By default, Stash@{0} always be the last latest stash.
Top tip: If higher the number like stash@{3}, stashes are saved earlier. Latest stashes always have lower number
WIP On fake - Means WIP On Branch Name (fake is just a branch name like any other branch) and WIP stands for Work In Progress. Which infers that the stash(stash@{0}) created on the branch(fake branch).
fc99b30 add head line - Commit hash and commit message. At that time of latest commit while stash creation.
show the latest stash
May be we have multiple stashes in our stash stack. We can't recognize which stash references conceals which changes. So, Before apply stashes on current working branch. Just for confirmation, Show the changes recorded in stash by below command.
git stash show
By default git stash show
show the changes recorded in latest stash as --stat
format.
--stat
format shows only how many lines added and deleted on file basis.
readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
want to show the recorded changes of latest stash on patch view, Use -p
flag on the end of the command
git stash show -p
Want to show untracked files also. Use -u
flag.
git stash show -u
or use --include-untracked
flag
git stash show --include-untracked
Show untracked files also with patch format
git stash show -p -u
Show only untracked files with patch format
git stash show -p --only-untracked
Wow, You already learned 10+ stash commands
Keep continue Reading..
Show the particular stash
Show the recorded changes for particular stash by using stash reference.
git stash show stash@{1}
For patch format, you guessed right. Use -p
flag
git stash show stash@{1} -p
Want to show with untracked files.
git stash show stash@{1} -u
git stash show stash@{1} --include-untracked
For untracked files only
git stash show stash@{1} --only-untracked
Apply the stash
Apply the recorded changes of latest stash on current working branch and remove that stash from stash stack.
git stash pop
Apply the latest stash Without removing of stash from stash stack.
git stash apply
Apply earlier stash by using stash reference
git stash apply stash@{1}
Delete the stash
Want to clear all the stashes from stash stack. Use below command
git stash clear
Want to delete a particular stash. Yes! you are right. Use stash reference.
git stash drop stash@{2}
Yes, We can create the new branch from latest stash.
git stash branch <branch_name>
For instance,
git stash branch demo
If we want to create the branch from earlier stash, That also possible
git stash branch <branch_name> stash@{revision}
For instance,
git stash branch purple stash@{3}
Such a long travel with you. But, It's really informative right.
Hope you this and today you get a new thing with you. Relax and take a coffee or what drink you love.
It's really take so much of time to understood from git docs and spend a lot of time for show this commands to you in understandable way.
If you share your and thoughts, which really making me more cheerful.
Original Link: https://dev.to/preethi_dev/mastering-on-20-git-stash-commands-3h29
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To