Вернитесь в спрятанную версию git - PullRequest
0 голосов
/ 10 июня 2018

Я был на ветке и внес некоторые изменения.Затем я спрятал изменения в ветке и затем создал новую ветку через:

git stash save "message"

git checkout -b newbranch oldbranch

Теперь я хочу вернуться к той версии, в которой я находился, когда спрятал, как мне это сделать?Я также не внес никаких изменений в новую ветку, поэтому мне все равно, потеряю ли я какую-либо информацию из новой ветки, я просто удалю ее.

1 Ответ

0 голосов
/ 10 июня 2018

Это зависит от того, что вы подразумеваете под

. Вернитесь к той версии, в которой я находился, когда спрятал

. Если вы хотите получить спрятанные изменения, вы можетеиспользуйте либо git stash pop, либо git stash apply.Разница в том, что pop удалит изменения из тайника, а apply оставит изменения в тайнике (т. Е. Чтобы их можно было применить снова).

Если вы хотите переключиться обратно на oldbranch, вы можете использовать git checkout oldbranch.


Вот пример рабочего процесса:

# start a fresh repository
$ git init
Initialized empty Git repository in /home/chuckx/code/stackoverflow/git-stash/.git/

# start a fresh branch
$ git checkout -b branch1
Switched to a new branch 'branch1'

# populate a file with content and commit it
$ echo branch1 content > file.txt
$ git add file.txt
$ git commit -m "branch1 content"
[branch1 (root-commit) dadf402] branch1 content
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt

# make a post-commit change to the file
$ echo stashed content >> file.txt
$ git status
On branch branch1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")

# stash the post-commit change
$ git stash save
Saved working directory and index state WIP on branch1: dadf402 branch1 content

# verify that we're back to a clean working tree with no changes
$ git status
On branch branch1
nothing to commit, working tree clean

# start a new branch
$ git checkout -b branch2
Switched to a new branch 'branch2'

# make a change
$ echo branch2 content >> file.txt
$ git status
On branch branch2
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ cat file.txt
branch1 content
branch2 content


# decide that we do not want to commit any changes to the new branch

# switch back to the original branch
$ git checkout branch1
M       file.txt
Switched to branch 'branch1'

# notice that our unstaged changes carry over after the branch switch
$ git status
On branch branch1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ cat file.txt
branch1 content
branch2 content


# perform a checkout of all files to fetch files as we left them in
# in the original branch
$ git checkout .
$ git status
On branch branch1
nothing to commit, working tree clean
$ cat file.txt
branch1 content

# retrieve the stashed changes and apply them over the fresh orignal
# branch checkout
$ git stash pop
On branch branch1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (9a326f0ff35f65313da479c742b624870807f550)
$ cat file.txt
branch1 content
stashed content
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...