如何在多个不同的分支上工作,以便在它们之间轻松切换? [英] How to work on the multiple different branches where I can switch easily between them?

查看:133
本文介绍了如何在多个不同的分支上工作,以便在它们之间轻松切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在GIT中使用相同的文件但使用不同的功能/分支?

Is there a way to work on the same file but on different features/branches in GIT?

我敢肯定有办法,但是最简单的是什么?
我不想隐瞒我的更改,因为这很麻烦.

I'm sure there is a way but what is the easiest?
I don't want to stash my changes as that's cumbersome.

使用SVN,我可以在2个不同的分支上作为2个不同的实体工作,而无需任何干预,并且可以轻松地在这两个分支之间进行切换.

With SVN I was able to work on 2 separate branches as 2 different entities without any intervention and easy to switch between the two.

推荐答案

使用git工作树.

Git工作树于2007年在git repo中的contrib文件夹下引入,并称为new-workdir.

Git worktree was introduced in 2007 under the contrib folder in the git repo and was called new-workdir.

git V2.5 中,它被命名为worktree,它使您可以在不同文件夹中拥有同一存储库的多个实例.

In git V2.5 it was named worktree and it allows you to have multiple instances of the same repository across different folders.

例如:

git worktree add <second path>

将在您的计算机上创建另一个文件夹,该文件夹使您可以同时在不同的分支上工作.

will create another folder on your computer which allows you to work on different branch simultaneously.

如果要删除工作树,请删除文件夹,然后执行git worktree prune,这将删除工作树引用.

If you want to remove the worktree, delete the folder and then execute git worktree prune which will remove the worktree reference.

prune
$GIT_DIR/worktrees中修剪工作树信息.

prune
Prune working tree information in $GIT_DIR/worktrees.

创建新的工作树

# create new branch inside the worktree folder 
git worktree -b <branch name> <path>

删除工作树

# do your code and once you have done 
# commit, push and now you can delete your folder
rm -rf <path>

# Tell git to remove the workdir copy
git worktree prune

更新 在以后的版本(git 2.17+)中,git worktree delete将作为删除工作树的新命令公开.

Update In the coming versions (git 2.17+) a git worktree delete will be exposed as a new command for deleting worktrees.

如果稍后使用rebase:

If you use rebase later on:

  • 注意:(自Git 2.7起)
    您也可以使用git rebase [--no]-autostash.
  • Note: (Since Git 2.7)
    you can also use the git rebase [--no]-autostash as well.

这篇关于如何在多个不同的分支上工作,以便在它们之间轻松切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆