简体   繁体   English

git在分支之间快速切换的最佳实践

[英]git best practices for quickly switching between branches

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? 是这样做的唯一方法,而无需为了从一个分支切换到另一个分支而“提交”和“签出”?

If you are temporarily switching branches git stash is useful, however, remember that commits don't need to persist forever; 但是,如果您要临时切换分支,则git stash很有用,请记住,提交并不需要永久保存; you may make temporary commits to roll back later. 您可以进行临时提交以在以后回滚。

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 message using git stash save otherwise it can be difficult to find later. 并且,由于这是关于最佳实践的问题,因此请记住使用git stash save 给您的存储区一个有用的消息,否则以后可能很难找到。

是的,但是如果您不准备完成当前正在进行的工作,则可以使用git stash而不是commit

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). 我通常将一个本地裸存储库克隆为多个副本(每个活动分支一个),然后将该裸存储库用作中央集成存储库(因为我可以轻松地推送到裸存储库,而不能推送到非裸存储库)。

I got tired of switching between branches and so I wrote a smarter git checkout . 我已经厌倦了在分支之间进行切换,所以我写了一个更聪明的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. 将以下内容插入~/.bash_profile ,获取其来源,然后只需使用gch切换到您所在的最后一个分支即可。

current_git_branch() {
    git branch | grep \* | awk '{ print $2 }'
}
# a smart git checkout, with no args it switches to last branch.
gch() {
    if [ -n "$1" ]; 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
}

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. Git可以通过涂抹和清洁来帮助解决此问题。 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. 如果在任何时候都提交了配置文件,则可以通过从连接中删除分支的名称来对其进行清理。

For more information, take a look at the Pro Git book . 有关更多信息,请参阅Pro Git书

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中的分支,此方法就起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM