在分支之间快速切换的 git 最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3055492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git best practices for quickly switching between branches
提问by Richard
I have multiple active branches that I need to work on at the same time. Clearly I can create two working directories with a distinct branch per directory. Is that the only way to do it without having to "commit" and "checkout" in order to switch from one branch to another?
我有多个需要同时处理的活动分支。显然,我可以创建两个工作目录,每个目录都有一个不同的分支。这是唯一一种无需“提交”和“结帐”即可从一个分支切换到另一个分支的方法吗?
采纳答案by dahlbyk
Yes, though you can use git stash
instead of commit
if you're not ready to finalize your current work in progress.
是的,但如果您还没有准备好完成当前正在进行的工作,您可以使用git stash
代替commit
。
回答by cweider
If you are temporarily switching branches git stash
is useful, however, remember that commits don't need to persist forever; you may make temporary commits to roll back later.
git stash
但是,如果您临时切换分支很有用,请记住提交不需要永远持续;您可以进行临时提交以稍后回滚。
So my recommendation is, if it is a many hours long switch, to do a git commit
instead, because, depending on your memory, stashes can be easy to forget/lose/etc.
所以我的建议是,如果切换需要几个小时,请改为执行git commit
,因为根据您的记忆力,隐藏文件很容易忘记/丢失/等。
[In MyBranch]
>$ git commit -m "WIP: Stuff I was working on."
>$ git checkout AnotherBranch
[Do Stuff]
>$ git checkout MyBranch
>$ git reset HEAD^
[Continue]
And since this is a question about best practices, remember to give your stash a useful messageusing git stash save
otherwise it can be difficult to find later.
由于这是一个关于最佳实践的问题,请记住使用向您的 stash 提供有用的消息,git stash save
否则以后可能很难找到。
回答by VonC
git clone
, through the local protocol, is a good alternative to be able to work on multiple branches at the same time.
git clone
,通过本地协议,是一个很好的替代方案,可以同时在多个分支上工作。
I usually clone one local bare repo into multiple copies (one for each active branch), and use that bare repo as a central integration repo (since I can push easily to a bare repo, versus not being able to push to non-bare repo).
我通常将一个本地裸仓库克隆成多个副本(每个活动分支一个),并将该裸仓库用作中央集成仓库(因为我可以轻松推送到裸仓库,而不是无法推送到非裸仓库)。
回答by skensell
I got tired of switching between branches and so I wrote a smarter git checkout
. Insert the following into your ~/.bash_profile
, source it, and then simply use gch
to switch to the last branch you were on.
我厌倦了在分支之间切换,所以我写了一个更聪明的git checkout
. 将以下内容插入到您的~/.bash_profile
, source 中,然后简单地使用gch
切换到您所在的最后一个分支。
current_git_branch() {
git branch | grep \* | awk '{ print }'
}
# a smart git checkout, with no args it switches to last branch.
gch() {
if [ -n "" ]; then
echo `current_git_branch` >"/tmp/last_git_branch_used.txt"
git checkout "$@"
else
if [ ! -f "/tmp/last_git_branch_used.txt" ]; then echo >&2 "ERROR: Please run gch with 1 argument first."
else
echo `current_git_branch` >"/tmp/last_git_branch_used.temp"
git checkout `cat /tmp/last_git_branch_used.txt`
mv "/tmp/last_git_branch_used."{temp,txt}
fi
fi
}
回答by Adam Dymitruk
If you are doing what is called branch-per-feature development as explained here:
如果您正在执行所谓的按功能分支开发,如下所述:
http://martinfowler.com/bliki/FeatureBranch.html
http://martinfowler.com/bliki/FeatureBranch.html
you might want to also ensure that you switch the database schemas. Git can help in this by means of smudge and clean. Managing multiple databases locally is then possible. When you checkout a new branch, you smudge the connection string to annotate the database name with the branch name. Should the configuration file be committed at any point, it is cleaned by removing the name of the branch from the connection.
您可能还想确保切换数据库模式。Git 可以通过涂抹和清理来帮助解决这个问题。这样就可以在本地管理多个数据库。当您签出新分支时,您会涂抹连接字符串以使用分支名称注释数据库名称。如果配置文件在任何时候被提交,它会通过从连接中删除分支的名称来清理。
For more information, take a look at the Pro Git book.
有关更多信息,请查看Pro Git 书籍。
回答by Gismo Ranas
I have a bash function like this:
我有一个像这样的 bash 函数:
function gitredocommit {
lastcomment=`git log | grep Date -A 2 -m 1 | tail -1 | sed -e 's/^ *//' -e 's/ *$//' | grep -v Merge`
if [ -n "$lastcomment" ]; then
git reset --soft HEAD^; git add ../; git commit -m"$lastcomment"
else
echo "last commit was a merge, won't redo it"
fi
}
You create a new branch, make a first (and last) commit and then with this you can do new stuff and overwrite this commit. If you need to update from the master you do it with
您创建一个新分支,进行第一个(也是最后一个)提交,然后您可以做新的事情并覆盖这个提交。如果您需要从主人更新,请使用
git rebase master
of course, so your commit is always on top in the branch. This works as long as you don't merge the branch in master.
当然,所以你的提交总是在分支的顶部。只要您不在 master 中合并分支,这就会起作用。