Git Stash
Введение | |
Пример | |
stash save | |
stash list | |
stash apply | |
stash pop | |
rebase + stash: Разрешение конфликта | |
Другие статьи про Git |
Введение
В этой статье вы можете узнать про применение команды git stash.
Сначала даны общие соображения, затем информация конкретно про команду
git stash
Пример
В master закоммичен файл calc.py
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): pass
git checkout -b add
Switched to a new branch 'add'
Вносим изменения в код функции add()
def add(a, b): return a + b …
git diff calc.py
warning: in the working copy of 'calc.py', LF will be replaced by CRLF the next time Git touches it diff --git a/calc.py b/calc.py index 74480ae..92116e4 100644 --- a/calc.py +++ b/calc.py @@ -1,5 +1,5 @@ def add(a, b): - pass + return a + b
Если сейчас сделать чекаут в мастер, никакой ошибки не будет. Файл будет виден как изменённый и в мастере.
Проблема была бы если бы кто-то подсуетился и отредактировал файл в мастере пока мы сидели в add, но сейчас всё хорошо.
git checkout master
M calc.py Switched to branch 'master'
git status
On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: calc.py no changes added to commit (use "git add" and/or "git commit -a")
Вернёмся в add и сделаем там stash
git checkout add
M calc.py Switched to branch 'add'
git stash save "Worked on add function"
warning: in the working copy of 'calc.py', LF will be replaced by CRLF the next time Git touches it Saved working directory and index state On add: Worked on add function
git diff
git status
On branch add nothing to commit, working tree clean
Изменения в файле отменены
def add(a, b): pass …
Чтобы изучить список существующих стэшей используют команду list
git stash list
stash@{0}: On add: Worked on add function
Чтобы подтянуть данные из стэша используют команду apply
git stash apply stash@{0}
On branch add Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: calc.py no changes added to commit (use "git add" and/or "git commit -a")
Изменения вернулись.
def add(a, b): return a + b …
Сам стэш не удалён
git stash list
stash@{0}: On add: Worked on add function
git checkout -- .
Изменения в файле отменены
def add(a, b): pass …
Сам стэш по-прежнему на месте.
git stash list
stash@{0}: On add: Worked on add function
С помощью pop можно применить последний из сделанных стэшей и удалить его
git stash pop
On branch add Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: calc.py no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (2a2b5c77599d40413526b90929787c3b24d48158)
Изменения вернутся а сам стэш удалится.
git stash list
git diff calc.py
warning: in the working copy of 'calc.py', LF will be replaced by CRLF the next time Git touches it diff --git a/calc.py b/calc.py index 74480ae..92116e4 100644 --- a/calc.py +++ b/calc.py @@ -1,5 +1,5 @@ def add(a, b): - pass + return a + b
Внесем новые изменения в файл
def add(a, b): return a + b def subtract(a, b): return a - b def multipy(a, b): return a * b def divide(a, b): return a / b
git diff
diff --git a/calc.py b/calc.py index 74480ae..073159f 100644 --- a/calc.py +++ b/calc.py @@ -1,12 +1,12 @@ def add(a, b): - pass + return a + b def subtract(a, b): - pass + return a - b def multipy(a, b): - pass + return a * b def divide(a, b): - pass + return a / b
git stash save "Calc Functions"
Saved working directory and index state On add: Calc Functions
git stash list
stash@{0}: On add: Calc Functions
Изменения в файле отменены
def add(a, b): pass …
Добавим функцию square()
… def square(a, b): pass
git diff
diff --git a/calc.py b/calc.py index 74480ae..f6e6a9e 100644 --- a/calc.py +++ b/calc.py @@ -10,3 +10,5 @@ def multipy(a, b): def divide(a, b): pass +def square(a, b): + pass
git stash save "Added Square function"
Saved working directory and index state On add: Added Square function
git stash list
stash@{0}: On add: Added Square function stash@{1}: On add: Calc Functions
git stash pop
On branch add Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: calc.py no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (6766bae510aad02144b79b4e03d4a58defe6479e)
git diff
diff --git a/calc.py b/calc.py index 74480ae..f6e6a9e 100644 --- a/calc.py +++ b/calc.py @@ -10,3 +10,5 @@ def multipy(a, b): def divide(a, b): pass +def square(a, b): + pass
git stash list
stash@{0}: On add: Calc Functions
git stash save "Added Square function"
Saved working directory and index state On add: Added Square function
git stash list
stash@{0}: On add: Added Square function stash@{1}: On add: Calc Functions
git stash drop stash@{0}
Dropped stash@{0} (1650ddf3181da30f0842dc3c103590c132cb34f0)
git stash list
stash@{0}: On add: Calc Functions
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): pass
Вернём square() вручную и застэшим
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): pass def square(a, b): pass
git stash save "Added Square function"
Saved working directory and index state On add: Added Square function
git stash list
stash@{0}: On add: Added Square function stash@{1}: On add: Calc Functions
git stash clear
Файл вернулся в исходное состояние.
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): pass
git stash list
git status
On branch add nothing to commit, working tree clean
git checkout master
Switched to branch 'master'
git status
On branch master nothing to commit, working tree clean
Вносим изменения в код функции add()
def add(a, b): return a + b …
git status
On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: calc.py no changes added to commit (use "git add" and/or "git commit -a")
git diff
warning: in the working copy of 'calc.py', LF will be replaced by CRLF the next time Git touches it diff --git a/calc.py b/calc.py index 74480ae..92116e4 100644 --- a/calc.py +++ b/calc.py @@ -1,5 +1,5 @@ def add(a, b): - pass + return a + b
git checkout add
M calc.py Switched to branch 'add'
git status
On branch add Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: calc.py no changes added to commit (use "git add" and/or "git commit -a")
Разрешение конфликта с помощью stash
Наша ветка создана из мастера в момент, когда файл был таким
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): pass
Вернёмся в master и добавим return в divide()
git checkout master
M calc.py Switched to branch 'master'
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): return a / b
git status
On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: calc.py no changes added to commit (use "git add" and/or "git commit -a")
git add calc.py
git commit -m "Adding return to divide"
[master 1c57998] Adding return to divide 1 file changed, 2 insertions(+), 2 deletions(-)
Перейдём в ветку add и там добавим return в add()
git checkout add
Switched to branch 'add'
git status
On branch add nothing to commit, working tree clean
cat calc.py
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): pass
Отредактируем код
def add(a, b): return a + b def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): pass
git checkout master
error: Your local changes to the following files would be overwritten by checkout: calc.py Please commit your changes or stash them before you switch branches. Aborting
Теперь в мастере return есть у subtract() а в ветке add return есть у add() и безболезненно переключиться нельзя
git stash save "Adding return to add()"
Saved working directory and index state On add: Adding return to add()
git stash list
stash@{0}: On add: Adding return to add()
git checkout master
Switched to branch 'master'
git stash list
stash@{0}: On add: Adding return to add()
git checkout add
Switched to branch 'add'
git rebase master
Successfully rebased and updated refs/heads/add.
Из мастера подтянулся return для divide
vi calc.py
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): return a / b
git stash pop
Auto-merging calc.py CONFLICT (content): Merge conflict in calc.py On branch add Unmerged paths: (use "git restore --staged <file>..." to unstage) (use "git add <file>..." to mark resolution) both modified: calc.py no changes added to commit (use "git add" and/or "git commit -a") The stash entry is kept in case you need it again.
cat calc.py
def add(a, b): <<<<<<< Updated upstream pass ======= return a + b >>>>>>> Stashed changes def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): return a / b
Теперь вручную редактируем файл оставляя то что над >>>>>>> Stashed changes
vi calc.py
def add(a, b): return a + b def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): return a / b
git status
On branch add Unmerged paths: (use "git restore --staged <file>..." to unstage) (use "git add <file>..." to mark resolution) both modified: calc.py no changes added to commit (use "git add" and/or "git commit -a")
git add calc.py
git status
On branch add Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: calc.py
git commit -m "Adds return to add()"
[add 15a3130] Adds return to add() 1 file changed, 1 insertion(+), 1 deletion(-)
git status
On branch add nothing to commit, working tree clean
git checkout master
Switched to branch 'master'
vi calc.py
def add(a, b): pass def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): return a / b
git merge -m "Merging add branch to master" add
Updating 1c57998..15a3130 Fast-forward (no commit created; -m option ignored) calc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
git status
On branch master nothing to commit, working tree clean
vi calc.py
def add(a, b): return a + b def subtract(a, b): pass def multipy(a, b): pass def divide(a, b): return a / b
Автор статьи: Андрей Олегович
Git | |
Установка | |
Основы | |
branch: Ветки | |
stash | |
Перейти с HTTPS на SSH | |
.gitignore | |
Необходимые Bash команды | |
Remote | |
GitHub | |
GitLab | |
Ошибки | |
Git Bash | |
Работа с API GitHub |