3

Mastering on 20+ git stash commands🎉

 2 years ago
source link: https://dev.to/preethi_dev/mastering-on-20-git-stash-commands-3h29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for Mastering on 20+ git stash commands🎉
⚡Preethi

Posted on Mar 23

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

Enter fullscreen mode

Exit fullscreen mode

git stash save

Enter fullscreen mode

Exit fullscreen mode

Stash the untracked files❗

Want to stash the untracked files. Just use -u flag on end of the command

git stash -u

Enter fullscreen mode

Exit fullscreen mode

or use --include-untracked on end of the command

git stash --include-untracked

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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(-)

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

Want to show untracked files also. Use -u flag.

git stash show -u

Enter fullscreen mode

Exit fullscreen mode

or use --include-untracked flag

git stash show --include-untracked

Enter fullscreen mode

Exit fullscreen mode

Show untracked files also with patch format

git stash show -p -u

Enter fullscreen mode

Exit fullscreen mode

Show only untracked files with patch format

git stash show -p --only-untracked

Enter fullscreen mode

Exit fullscreen mode

Wow, You already learned 10+ stash commands🎉🎉

Keep continue Reading..

Image description

Show the particular stash📺

Show the recorded changes for particular stash by using stash reference.

git stash show stash@{1}

Enter fullscreen mode

Exit fullscreen mode

For patch format, you guessed right👏🏻👏🏻. Use -p flag

git stash show stash@{1} -p

Enter fullscreen mode

Exit fullscreen mode

Want to show with untracked files.

git stash show stash@{1} -u

Enter fullscreen mode

Exit fullscreen mode

git stash show stash@{1} --include-untracked

Enter fullscreen mode

Exit fullscreen mode

For untracked files only

git stash show stash@{1} --only-untracked

Enter fullscreen mode

Exit fullscreen mode

Apply the stash🖊️

Apply the recorded changes of latest stash on current working branch and remove that stash from stash stack.

git stash pop

Enter fullscreen mode

Exit fullscreen mode

Apply the latest stash Without removing of stash from stash stack.

git stash apply

Enter fullscreen mode

Exit fullscreen mode

Apply earlier stash by using stash reference

git stash apply stash@{1}

Enter fullscreen mode

Exit fullscreen mode

Delete the stash

Want to clear all the stashes from stash stack. Use below command

git stash clear

Enter fullscreen mode

Exit fullscreen mode

Want to delete a particular stash. Yes! you are right. Use stash reference.

git stash drop stash@{2}

Enter fullscreen mode

Exit fullscreen mode

Yes, We can create the new branch from latest stash.

git stash branch <branch_name>

Enter fullscreen mode

Exit fullscreen mode

For instance,

git stash branch demo

Enter fullscreen mode

Exit fullscreen mode

If we want to create the branch from earlier stash, That also possible🥳🥳

git stash branch <branch_name> stash@{revision}

Enter fullscreen mode

Exit fullscreen mode

For instance,

git stash branch purple stash@{3}

Enter fullscreen mode

Exit fullscreen mode

Image description
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.

-Preethi


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK