debian-mirror-gitlab/doc/university/training/topics/stash.md

80 lines
1.4 KiB
Markdown
Raw Normal View History

2018-03-17 18:26:18 +05:30
---
comments: false
---
2017-08-17 22:00:37 +05:30
# Git Stash
2019-12-21 20:55:43 +05:30
We use `git stash` to store our changes when they are not ready to be committed
2017-08-17 22:00:37 +05:30
and we need to change to a different branch.
2019-03-02 22:35:43 +05:30
- Stash:
2018-11-20 20:47:30 +05:30
2020-03-09 13:42:32 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash save
# or
git stash
# or with a message
git stash save "this is a message to display on the list"
```
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
- Apply stash to keep working on it:
2018-11-20 20:47:30 +05:30
2020-03-09 13:42:32 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash apply
# or apply a specific one from out stack
git stash apply stash@{3}
```
2017-08-17 22:00:37 +05:30
2019-10-12 21:52:04 +05:30
- Every time we save a stash it gets stacked so by using `list` we can see all our
2019-07-07 11:18:12 +05:30
stashes.
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash list
# or for more information (log methods)
git stash list --stat
```
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
- To clean our stack we need to manually remove them:
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
```shell
2019-10-12 21:52:04 +05:30
# drop top stash
git stash drop
# or
git stash drop <name>
# to clear all history we can use
git stash clear
```
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
- Apply and drop on one command:
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
```shell
2019-10-12 21:52:04 +05:30
git stash pop
```
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
- If we meet conflicts we need to either reset or commit our changes.
- Conflicts through `pop` will not drop a stash afterwards.
2017-08-17 22:00:37 +05:30
2019-10-12 21:52:04 +05:30
## Git Stash sample workflow
2017-08-17 22:00:37 +05:30
1. Modify a file
2019-02-15 15:39:39 +05:30
1. Stage file
1. Stash it
1. View our stash list
1. Confirm no pending changes through status
1. Apply with pop
1. View list to confirm changes
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
```shell
2017-08-17 22:00:37 +05:30
# Modify edit_this_file.rb file
git add .
git stash save "Saving changes from edit this file"
git stash list
git status
git stash pop
git stash list
git status
```