我想提交并关闭分支,但不从历史记录中删除它。使用Mercurial,我会使用
commit --close-branch
,然后update
到之前的某个版本,并继续工作。但是使用Git...我感到困惑。commit --close-branch
,然后update
到之前的某个版本,并继续工作。但是使用Git...我感到困惑。在Git中,没有关闭分支的确切等效方式,因为Git分支比Mercurial更轻量级。它们在Mercurial中的等效方式更像是书签而不是分支。
如果我理解正确,在Mercurial中关闭一个分支大致相当于从分支列表中将其删除,因此您可以通过归档来实现相同的效果。一个常见做法是将其末尾标记为archive,并将其删除:
git tag archive/<branchname> <branchname>
git branch -d <branchname>
git checkout master
该分支将被删除,可以通过检出标签并重新创建分支来稍后检索:
git checkout archive/<branchname>
git checkout -b new_branch_name
git branch -D
而不是 -d
。 - Tor Klingberg#Get all branches on remote and place in array. Format of strings in array is for example "3 months ago|origin/branch-name"
$branches = git branch -r --sort=-committerdate --format="%(committerdate:relative)|%(refname:short)|%(refname:lstrip=3)"
#Loop through all branches
ForEach ($branch in $branches)
{
#split the branch between last commit time and branch name
$split = $branch.Split("|")
try
{
#check if the last commit date is 4 months or more
if($split[0] -match "((^([4-9]|10|11|12) month)|year)")
{
$splitBranch = $split[1].Split("/")
#tag the branch
git tag archive/$split[2] $split[1]
#delete the branch
git push --delete $splitBranch[0] $split[2]
#add the archived branch name to a text file
Add-Content .\archived.txt $split[1]
}
}
catch
{
#log any branches that failed
Add-Content .\archiveFailures.txt $split[1]
}
}
#push all newly created tags
git push --tags
#to restore archived branch
#git checkout -b <branchname> archive/<branchname>
我的经验是Mercurial对关闭分支有很好的支持,举个例子。
Git也许希望你要么删除分支以保持可维护性,减少分支计数,要么坚持使用标签。只要你有一个标签,就可以将其恢复为分支。我使用别名命令和shell函数,使关闭分支变得容易(将其归档为标签以备后用,如果我再次找到它有用)。
我建议你只关闭你确定以后会有用的分支。当恢复分支并尝试合并时,需要进行大量的合并修复工作的可能性很高。但也可能只是想查看旧分支中代码的状态,在“删除”它但保留为标签的情况下保留它。
我在我的.gitconfig文件中使用这种方法,放在[alias]下面。
closebranch = "!w() { echo Attempting to close local and remote branch: $1 Processing...; echo Checking the branch $1 out..; git checkout $1; echo Trying to create a new tag archive/$1; git tag archive/\"$1\"; git push origin archive/\"$1\"; echo Deleting the local branch $1; git branch -d $1; echo Deleting the remote branch $1; git push origin --delete $1; echo Done. To restore the closed branch later, enter: git checkout -b MyNewBranch archive/\"$1\"; }; w"
closebranchpassive = "!w() { echo Attempting to close local and remote branch: $1 Processing...; echo Checking the branch $1 out..; git checkout $1; echo Trying to create a new tag archive/$1; git tag archive/\"$1\"; git push origin archive/$1; echo Deleting the local branch $1; echo Deleting the remote branch $1; echo Done. To restore the closed branch later, enter: git checkout -b MyNewBranch archive/\"$1\"; }; w"
closeremotebranch = "!w() { echo Attempting to close remote branch: $1 Processing...; echo Checking the branch $1 out..; git checkout $1; echo Trying to create a new tag archive/$1; git tag archive/\"$1\"; git tag archive/\"$1\"; echo Deleting the remote branch $1; git push origin --delete $1; echo Done. To restore the closed branch later, enter: git checkout -b MyNewBranch archive/\"$1\"; }; w"
git closebranch feature/#123-somefeature-branch
您最终将处于分离状态,在其中您将看到如何稍后恢复您的“关闭”分支,现在它作为远程标签存在,可以检出并恢复为分支,例如: git checkout -b feature/#123-somefeature-branch-restoredalive-again archive/feature/#123-somefeature-branch