Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 23, 2022 03:41 pm GMT

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,

Image description

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

Image description

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}

Image description

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}

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

Image description


Original Link: https://dev.to/preethi_dev/mastering-on-20-git-stash-commands-3h29

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To