如何备份当前状态(包括未跟踪的文件),以便我以后可以检查它们
How to take backup of the current state (including untracked files) so that if i want i can check them later
我目前正在提交
我有一些修改过的文件和未跟踪的文件
我想保留当前状态的备份以备将来参考。
我已经阅读并发现主要使用 stash。
我希望现阶段的任何文件都应该保存到藏匿处。 (包括未跟踪的也不要删除git忽略文件)
我发现 git 存储正在被使用。它会将当前状态保存到 stash 中,并且在 stashing 之后会有一个干净的工作树
以下哪种方式使用git stash。
$ git stash save "my_stash"
$ git git stash --include-untracked save "my_stash"
$ git stash --all save "my_stash"
然后我们使用
再次取回存储
$ git apply
此外,如果可能的话,我想知道如何通过创建分支而不是存储来做同样的事情。
请注意,当您说 "at the current stage" 时,这显然不是您的意思。 (staged
在 git-speak 中表示 'in the index';这肯定会 不 包括未跟踪的文件 - 也不包括对跟踪文件的未暂存更改。)
至于用什么命令,应该不用git stash save
;它已被弃用,因此您应该改用 git stash push
。 (push
是默认值,因此您在创建存储时根本不必命名任何子命令。)
我不确定您希望如何处理被忽略的文件是什么意思。如果你想忽略的文件包含在 stash
git stash --all
如果您不希望隐藏文件中的忽略文件(因此希望它们保留在工作树中)
git stash -u
当你 apply
存储时,你需要让 git 知道你想要恢复索引
git stash apply --index
请注意,这仅在隐藏应用过程不产生冲突时有效;对于这种用途,它不应该冲突,但是如果您申请与创建存储的地方不同的提交,它可以。
要创建 b运行ch 而不是存储:
您可以在此处使用一个快捷方式,因为存储实际上是具有经过特殊处理的引用的提交集合。所以你可以像往常一样创建存储,然后
git checkout stash
git checkout -b my_branch
git stash drop
b运行ch 看起来有点奇怪,因为 stash 以一种有点不寻常的方式使用合并 assemble 未跟踪、未暂存和已暂存的变更集。
如果您根本不想使用 stash 命令,那就更难了。您只能提交暂存的更改,因此您必须执行类似
的操作
git checkout -b my_branch
git commit -m "staged changes"
git commit -a -m "unstaged changes"
# if you want a commit with only the non-ignored
# untracked files
git add :/:
git commit -m "untracked files'
# if you want a single commit with all untracked
# files (ignored or not), then skip the previous
# command and run this; if you want a separate
# commit for ignored files, run the previous
# command and *then* run this:
git add -f :/:
git commit -m "ignored files"
(请注意,您甚至可以将未暂存、未跟踪和忽略的文件全部合并到一次提交中(尽管 stash 不会);但您确实必须单独提交已暂存的更改,以便稍后准确恢复索引.)
那你需要恢复工作状态,继续工作。要使用的确切命令取决于您 运行 以上命令中的哪一个。首先你想从你的藏匿式barnch
中分离出来
git checkout --detach
然后使用分阶段更改恢复索引。如果您创建了所有 4 个提交,这将是
git reset --mixed HEAD~3
如果您只为未跟踪的文件创建了一个提交
git reset --mixed HEAD~2
(或者,如果您只为未暂存的更改创建了一个提交 和 untracked/ignored 文件,它将是
git reset --mixed HEAD~1
或同等学历。)
然后 HEAD
离开 b运行ch
git reset --soft HEAD~1
终于再次检查您的工作 b运行ch;例如如果你从 master
开始
git checkout master
完成所有这些后,您已经将所有相同的信息存储为 stash;但是你仍然没有 stash 命令的自动化来帮助你,所以将来如果你想再次恢复那个状态,你将不得不清理工作树,检查类似 stash 的 b运行ch,然后再次重复整个 detach/reset/checkout 过程。
如果你想将这些更改应用到不同的基础提交(stash
的股票功能),你必须首先将类似存储的 b运行ch 重新设置为新提交,然后恢复那里的更改。
Mark 的回答很好,所以我就补充一下:
如果您发现自己经常处于这个位置,想要同时签出多个分支以比较更改并处理不同的更改,那么您可能需要使用 work trees。它们使签出多个分支变得容易,而无需多个本地存储库的开销(这在磁盘上可能会变得非常昂贵)。您还可以使用工作树临时切换到另一个分支,做一些工作,然后返回,而无需创建临时分支或记住您隐藏了一些更改。
这就是我要找的东西
我目前在 masters 分支,我正在处理一些未暂存的文件和一些未跟踪的文件。我想将我当前的状态保存到一个新分支中,作为将来的 backup/reference。
所以我在当前状态下做了 stash push --include-untracked
然后 create and checkout to a new branch
和 stash apply
并通过 add -A
和 commit changes
暂存所有文件。再次 come back to master branch
并在那里再次 stash apply
。
有
$ git status
On branch master
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
no changes added to commit (use "git add" and/or "git commit -a")
$ git stash push --include-untracked
One stash is run
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
$ git checkout -b backup_branch
Switched to a new branch 'backup_branch'
$ git stash apply
On branch backup_branch
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
$ git status
On branch backup_branch
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
$ git add -A
$ git commit -m "Saved for backup"
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git stash apply
On branch origin/master
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
$ git status
On branch origin/master
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
现在我回到了原来的工作目录。在新分支中备份我当前的状态 backup_branch
我目前正在提交
我有一些修改过的文件和未跟踪的文件
我想保留当前状态的备份以备将来参考。
我已经阅读并发现主要使用 stash。
我希望现阶段的任何文件都应该保存到藏匿处。 (包括未跟踪的也不要删除git忽略文件)
我发现 git 存储正在被使用。它会将当前状态保存到 stash 中,并且在 stashing 之后会有一个干净的工作树
以下哪种方式使用git stash。
$ git stash save "my_stash"
$ git git stash --include-untracked save "my_stash"
$ git stash --all save "my_stash"
然后我们使用
再次取回存储$ git apply
此外,如果可能的话,我想知道如何通过创建分支而不是存储来做同样的事情。
请注意,当您说 "at the current stage" 时,这显然不是您的意思。 (staged
在 git-speak 中表示 'in the index';这肯定会 不 包括未跟踪的文件 - 也不包括对跟踪文件的未暂存更改。)
至于用什么命令,应该不用git stash save
;它已被弃用,因此您应该改用 git stash push
。 (push
是默认值,因此您在创建存储时根本不必命名任何子命令。)
我不确定您希望如何处理被忽略的文件是什么意思。如果你想忽略的文件包含在 stash
git stash --all
如果您不希望隐藏文件中的忽略文件(因此希望它们保留在工作树中)
git stash -u
当你 apply
存储时,你需要让 git 知道你想要恢复索引
git stash apply --index
请注意,这仅在隐藏应用过程不产生冲突时有效;对于这种用途,它不应该冲突,但是如果您申请与创建存储的地方不同的提交,它可以。
要创建 b运行ch 而不是存储:
您可以在此处使用一个快捷方式,因为存储实际上是具有经过特殊处理的引用的提交集合。所以你可以像往常一样创建存储,然后
git checkout stash
git checkout -b my_branch
git stash drop
b运行ch 看起来有点奇怪,因为 stash 以一种有点不寻常的方式使用合并 assemble 未跟踪、未暂存和已暂存的变更集。
如果您根本不想使用 stash 命令,那就更难了。您只能提交暂存的更改,因此您必须执行类似
的操作git checkout -b my_branch
git commit -m "staged changes"
git commit -a -m "unstaged changes"
# if you want a commit with only the non-ignored
# untracked files
git add :/:
git commit -m "untracked files'
# if you want a single commit with all untracked
# files (ignored or not), then skip the previous
# command and run this; if you want a separate
# commit for ignored files, run the previous
# command and *then* run this:
git add -f :/:
git commit -m "ignored files"
(请注意,您甚至可以将未暂存、未跟踪和忽略的文件全部合并到一次提交中(尽管 stash 不会);但您确实必须单独提交已暂存的更改,以便稍后准确恢复索引.)
那你需要恢复工作状态,继续工作。要使用的确切命令取决于您 运行 以上命令中的哪一个。首先你想从你的藏匿式barnch
中分离出来git checkout --detach
然后使用分阶段更改恢复索引。如果您创建了所有 4 个提交,这将是
git reset --mixed HEAD~3
如果您只为未跟踪的文件创建了一个提交
git reset --mixed HEAD~2
(或者,如果您只为未暂存的更改创建了一个提交 和 untracked/ignored 文件,它将是
git reset --mixed HEAD~1
或同等学历。)
然后 HEAD
离开 b运行ch
git reset --soft HEAD~1
终于再次检查您的工作 b运行ch;例如如果你从 master
开始git checkout master
完成所有这些后,您已经将所有相同的信息存储为 stash;但是你仍然没有 stash 命令的自动化来帮助你,所以将来如果你想再次恢复那个状态,你将不得不清理工作树,检查类似 stash 的 b运行ch,然后再次重复整个 detach/reset/checkout 过程。
如果你想将这些更改应用到不同的基础提交(stash
的股票功能),你必须首先将类似存储的 b运行ch 重新设置为新提交,然后恢复那里的更改。
Mark 的回答很好,所以我就补充一下:
如果您发现自己经常处于这个位置,想要同时签出多个分支以比较更改并处理不同的更改,那么您可能需要使用 work trees。它们使签出多个分支变得容易,而无需多个本地存储库的开销(这在磁盘上可能会变得非常昂贵)。您还可以使用工作树临时切换到另一个分支,做一些工作,然后返回,而无需创建临时分支或记住您隐藏了一些更改。
这就是我要找的东西
我目前在 masters 分支,我正在处理一些未暂存的文件和一些未跟踪的文件。我想将我当前的状态保存到一个新分支中,作为将来的 backup/reference。
所以我在当前状态下做了 stash push --include-untracked
然后 create and checkout to a new branch
和 stash apply
并通过 add -A
和 commit changes
暂存所有文件。再次 come back to master branch
并在那里再次 stash apply
。
有
$ git status
On branch master
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
no changes added to commit (use "git add" and/or "git commit -a")
$ git stash push --include-untracked
One stash is run
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
$ git checkout -b backup_branch
Switched to a new branch 'backup_branch'
$ git stash apply
On branch backup_branch
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
$ git status
On branch backup_branch
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
$ git add -A
$ git commit -m "Saved for backup"
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git stash apply
On branch origin/master
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
$ git status
On branch origin/master
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: filename1
Untracked files:
(use "git add <file>..." to include in what will be committed)
filename2
现在我回到了原来的工作目录。在新分支中备份我当前的状态 backup_branch