git 在切换分支时候不想提交文件,怎么办? git的文件储存功能

git 的储存功能。


原文:http://www.liaoxuefeng.com/ 


件开发中,bug就像家常便饭一样。有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除。

当你接到一个修复一个代号101的bug的任务时,很自然地,你想创建一个分支issue-101来修复它,但是,等等,当前正在dev上进行的工作还没有提交:

$ git status
# On branch dev
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   hello.py
#
# 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:   readme.txt
#

并不是你不想提交,而是工作只进行到一半,还没法提交,预计完成还需1天时间。但是,必须在两个小时内修复该bug,怎么办?

幸好,Git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作:

$ git stash
Saved working directory and index state WIP on dev: 6224937 add merge
HEAD is now at 6224937 add merge

现在,用git status查看工作区,就是干净的(除非有没有被Git管理的文件),因此可以放心地创建分支来修复bug。

首先确定要在哪个分支上修复bug,假定需要在master分支上修复,就从master创建临时分支:

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
$ git checkout -b issue-101
Switched to a new branch 'issue-101'

现在修复bug,需要把“Git is free software ...”改为“Git is a free software ...”,然后提交:

$ git add readme.txt 
$ git commit -m "fix bug 101"
[issue-101 cc17032] fix bug 101
 1 file changed, 1 insertion(+), 1 deletion(-)

修复完成后,切换到master分支,并完成合并,最后删除issue-101分支:

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
$ git merge --no-ff -m "merged bug fix 101" issue-101
Merge made by the 'recursive' strategy.
 readme.txt |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git branch -d issue-101
Deleted branch issue-101 (was cc17032).

太棒了,原计划两个小时的bug修复只花了5分钟!现在,是时候接着回到dev分支干活了!

$ git checkout dev
Switched to branch 'dev'
$ git status
# On branch dev
nothing to commit (working directory clean)

工作区是干净的,刚才的工作现场存到哪去了?用git stash list命令看看:

$ git stash list
stash@{0}: WIP on dev: 6224937 add merge

工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:

一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

另一种方式是用git stash pop,恢复的同时把stash内容也删了:

$ git stash pop
# On branch dev
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   hello.py
#
# 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:   readme.txt
#
Dropped refs/stash@{0} (f624f8e5f082f2df2bed8a4e09c12fd2943bdd40)

再用git stash list查看,就看不到任何stash内容了:

$ git stash list

你可以多次stash,恢复的时候,先用git stash list查看,然后恢复指定的stash,用命令:

$ git stash apply stash@{0}小结

修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场。


  • 23
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 在 Git 中,你可以使用以下步骤将本地分支的代码提交到其他分支,同时不修改关联分支: 1. 首先,确保你当前在要提交代码的本地分支上。可以使用命令 `git branch` 查看当前所在的分支,并使用命令 `git checkout <branch_name>` 切换到要提交代码的分支上。 2. 然后,使用命令 `git checkout -b <new_branch_name>` 创建一个新的本地分支,用于提交代码到其他分支。 3. 接着,将代码修改提交到新创建的本地分支上,使用命令 `git add .` 添加所有修改,并使用命令 `git commit -m "commit message"` 提交修改。 4. 最后,使用命令 `git push origin <new_branch_name>:<destination_branch_name>` 将新创建的本地分支上的代码提交到目标分支。这里的 `<new_branch_name>` 是新创建的本地分支名称,`<destination_branch_name>` 是要提交到的目标分支名称。 以上就是在 Git 中将本地分支的代码提交到其他分支,同时不修改关联分支的步骤。 ### 回答2: 在Git中,要将本地分支的代码提交到其他分支,但不修改关联分支,可以使用以下步骤: 1. 确保你当前所在的分支是要提交代码的分支。 - 可以使用 `git branch` 命令查看当前所在分支,并确保是要提交代码的分支。 2. 创建一个新的分支,用于存放将要提交的代码。 - 可以使用 `git branch <new_branch_name>` 命令创建一个新的分支,例如 `git branch feature_branch`。 3. 切换到新创建的分支。 - 可以使用 `git checkout <new_branch_name>` 命令切换到新创建的分支,例如 `git checkout feature_branch`。 4. 将本地分支的代码提交到新的分支。 - 可以使用 `git cherry-pick <commit_id>` 命令将指定的提交应用到当前分支,例如 `git cherry-pick abc123`。 - 如果要将多个提交应用到当前分支,可以使用 `git cherry-pick <commit_id1> <commit_id2>` 的形式。 5. 检查并解决任何代码冲突。 - 如果应用提交时出现冲突,需要手动解决冲突。 - 可以使用 `git status` 命令查看冲突的文件,然后手动修改这些文件。 6. 提交代码到新的分支。 - 可以使用 `git push origin <new_branch_name>` 命令将新的分支推送到远程仓库,例如 `git push origin feature_branch`。 通过以上步骤,你可以将本地分支的代码提交到其他分支,而不修改关联分支的代码。请确保在操作Git时仔细检查和理解每个命令的作用,避免意外操作导致数据丢失或不可逆的修改。 ### 回答3: 你可以使用以下步骤从本地分支提交代码到其他分支,而不会修改关联分支: 1. 首先,确保你已经在所选的本地分支上完成了你的更改和提交。可以使用以下命令查看你当前所在的分支: ``` git branch ``` 2. 然后,使用以下命令创建一个新的分支,用于存储你的更改: ``` git checkout -b new_branch_name ``` 这将基于当前所在的分支创建一个新的分支。可以将 `new_branch_name` 替换为你要的新分支名称。 3. 接下来,将你的更改提交到新创建的分支: ``` git add . git commit -m "Your commit message" ``` 运行这两个命令,将你的更改添加到暂存区,并提交到新的分支。注意替换 `"Your commit message"` 为你的提交信息。 4. 最后,使用以下命令将新分支推送到远程仓库的其他分支: ``` git push origin new_branch_name:target_branch_name ``` 这将把新分支的更改推送到远程仓库的目标分支上。替换 `new_branch_name` 为你的新创建的分支名称,`target_branch_name` 为你提交的目标分支的名称。 完成以上步骤后,你的更改将从本地分支提交到其他分支,而不会修改关联分支的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值