关键词:
#!/usr/bin/env ruby
#
# Get a list of merged branches
#
# You need to checkout the target remote branch before running. In other words,
# if your target branch is 'master', you have to have it on your local hard disk
# before you run this script, otherwise you will get an error like: `fatal:
# malformed object name master`. Git needs to have the branch available locally
# with latest changes pulled in order to find the branches that have/have-not
# been merged into it.
#
# To create a list of branch-names only, without date info
#
# git-merged-branches.rb --branch production --exclude-days 180 | cut -f1 -d'|'
#
# For help run: git-merged-branches.rb --help
require 'trollop'
require 'fileutils'
require 'colored'
require 'pry'
class GitMergedBranches
attr_accessor :branch_count, :options, :elapsed, :excluded_branches, :matched_count
include FileUtils
class BranchEntry
attr_accessor :name, :last_commit_date, :relative_last_commit, :last_commiter
def initialize(name, last_commit_date, relative_last_commit, last_commiter)
self.name = name
self.last_commit_date = last_commit_date
self.relative_last_commit = relative_last_commit
self.last_commiter = last_commiter
end
def <=>(other)
last_commit_date <=> other.last_commit_date
end
def to_s
"#name | #last_commit_date | #relative_last_commit by #last_commiter"
end
end
class << self
def collect_args(*_args)
opts = Trollop.options do
opt(
:branch,
'Base branch - list branches merged into this branch. Uses current branch if not specified.',
type: :string, short: 'b', required: false
)
opt(
:exclude_days,
'Exclude branches that have no commits within this many days',
type: :integer, short: 'x', required: false, default: 0
)
opt(
:color,
'Use colored output',
type: :boolean, short: 'c', required: false, default: true
)
end
# Set branch to current if not given on command line
opts[:branch] ||= `git rev-parse --abbrev-ref HEAD`.chomp
opts
end
def run
start_time = Time.now
opts = collect_args(ARGV)
instance = GitMergedBranches.new(opts)
instance.process
instance.elapsed = Time.now - start_time
instance.report_summary
end
end
def initialize(opts)
self.branch_count = 0
self.excluded_branches = []
self.options = opts
end
def color(string, clr)
if options[:color]
puts(string.send(clr))
else
puts(string)
end
end
def report_summary
puts
color(">>> Processed #branch_count branches in [#elapsed] seconds", :red)
color(">>> Branches with NO commits in the last #options[:exclude_days] days: [#matched_count]", :red)
color(">>> Branches with commits in the last #options[:exclude_days] days: [#excluded_branches.count]", :red)
end
def process
self.matched_count = matched_branches.count
color(">>> #matched_count remote branches that have been merged into `#options[:branch]` with no commits in the last #options[:exclude_days] days:", :green)
puts
puts matched_branches.sort
end
def merged_branches
@branches ||= begin
current_origin = nil
cmd = "git branch -r --merged #options[:branch]"
merged_list = `#cmd`.split("\n").collect(&:strip)
raise "Error running: #cmd. See output above ^^^" unless $?.exitstatus == 0
# find and delete the HEAD pointer and current branch origin
head_pointer = merged_list.grep(/ -> /).first
current_origin = head_pointer.split(/ -> /).last if head_pointer
merged_list.delete_if |elem| [head_pointer, current_origin].include?(elem)
end
end
def matched_branches
today = Date.today
@sorted ||= merged_branches.map do |branch|
self.branch_count += 1
date_strings = `git show --format="%ci|%cr" #branch | head -n 1`.chomp.split('|')
last_commit_date = Date.strptime(date_strings.first, '%Y-%m-%d')
days_old = (today - last_commit_date).to_i
if recent_branch?(days_old)
excluded_branches << branch
nil
else
BranchEntry.new(branch, last_commit_date, date_strings.last, last_commiter(branch))
end
end.compact
end
# Determine if a branch has any commits within the last options[:exclude_days] days
def recent_branch?(branch_age)
branch_age.to_i <= options[:exclude_days].to_i
end
def last_commiter(branch)
cmd = %(git show --format="%ai %ar by %an" #branch | head -n 1)
data = `#cmd`.split
data.last
end
end
GitMergedBranches.run
ruby用于列出已合并到指定分支的所有远程分支的脚本。(可选)使用最近提交排除分支。书面(代码片段)
查看详情
在 Git 中将选定提交从一个分支合并到另一个分支的更好方法
】在Git中将选定提交从一个分支合并到另一个分支的更好方法【英文标题】:WhichisbetterwaytomergeselectedcommitsfromonebranchtootherbranchinGit【发布时间】:2019-01-2111:03:06【问题描述】:我有Master和Develop分支,开发人员在发布时将其代码... 查看详情
如何将一个远程分支覆盖而不是合并到另一个分支?
】如何将一个远程分支覆盖而不是合并到另一个分支?【英文标题】:HowcanIoverwrite,notmerge,oneremotebranchintoanotherbranch?【发布时间】:2013-04-1619:30:54【问题描述】:我有两个分支。分期和测试版。暂存中有代码(包括文件),我根... 查看详情
使用 Git 将未合并的功能分支合并到另一个功能分支
】使用Git将未合并的功能分支合并到另一个功能分支【英文标题】:Mergenon-mergedfeaturebranchintoanotherfeaturebranchwithGit【发布时间】:2013-12-0509:14:08【问题描述】:我的公司有一个如下所示的Git工作流程:从原始分支创建功能分支(... 查看详情
将一个本地分支合并到另一个本地分支
】将一个本地分支合并到另一个本地分支【英文标题】:mergeonelocalbranchintoanotherlocalbranch【发布时间】:2016-11-0710:53:27【问题描述】:我有多个从master分支出来的分支(每个分支都在一个单独的子目录中)。Branch1:新开发,尚未... 查看详情
markdown将来自repo的分支合并到另一个repo中作为专用分支下的子目录(代码片段)
查看详情
idea中git分支未push的变更集如何合并到另一个分支
使用rebase命令 刚开始,A分支和B分支的代码是一样的,把A分支checkout为当前分支,并且修改了代码,进行【commit】和【push】,commit成功了,但是push没有权限。这个时候在checkout到B分支,之前的修改已经没有了,它被提交到... 查看详情
Git 在将两个分支合并到主(主)分支之前将一个开发人员分支合并到另一个开发人员分支
】Git在将两个分支合并到主(主)分支之前将一个开发人员分支合并到另一个开发人员分支【英文标题】:GitMergingonedeveloperbranchtoanotherdeveloperbranchbeforemergingbothbranchestomaster(themain)branch【发布时间】:2021-11-1313:59:40【问题描述】... 查看详情
git合并指定文件到另一个分支
经常被问到如何从一个分支合并特定的文件到另一个分支。其实,只合并你需要的那些commits,不需要的commits就不合并进去了。合并某个分支上的单个commit首先,用gitlog或sourcetree工具查看一下你想选择哪些commits进行合并,例如... 查看详情
git把一个分支上的某个提交合并到另一个分支
...规矩的拉一个分支,开发一个功能,等功能测试完成后,合并到主分支。有很多的场景都是很多人在同一个开发分支上开发,然后按照上线的实际需要,依次去上传自己的功能模块,这个功能模块的提交记录很可能是交叉提交的... 查看详情
如何在SVN中从一个分支合并到另一个分支并再次返回(双向合并)?
】如何在SVN中从一个分支合并到另一个分支并再次返回(双向合并)?【英文标题】:Howtomergefrombranchtobranchandbackagain(bidirectionalmerging)inSVN?【发布时间】:2010-09-0906:24:53【问题描述】:使用svnmerge.py工具可以在分支之间进行合并... 查看详情
Git:如何防止特定的提交被合并到另一个分支中?
】Git:如何防止特定的提交被合并到另一个分支中?【英文标题】:Git:HowcanIpreventaspecificcommitfrombeingmergedintoanotherbranch?【发布时间】:2016-07-2600:32:34【问题描述】:我们公司的Git工作流程如下:我们有一个master分支,一些feature/*... 查看详情
如何将多个提交合并到另一个分支上作为单个压缩提交?
】如何将多个提交合并到另一个分支上作为单个压缩提交?【英文标题】:HowcanImergemultiplecommitsontoanotherbranchasasinglesquashedcommit?【发布时间】:2011-07-1514:16:09【问题描述】:我有一个远程Git服务器,这是我想要执行的场景:我为... 查看详情
如何在 Git 中将特定提交从一个分支合并到另一个分支?
】如何在Git中将特定提交从一个分支合并到另一个分支?【英文标题】:HowdoImergeaspecificcommitfromonebranchintoanotherinGit?【发布时间】:2011-09-1608:30:32【问题描述】:我有BranchA,比BranchB早113次提交。但我只想将来自BranchA的最后10个... 查看详情
合并(用压扁)另一个分支的所有变化,作为一个单一的提交。(代码片段)
在Git中,有没有一种方法可以将一个分支的所有变更合并到另一个分支,但同时将其压制为一个提交?我经常在一个单独的分支中开发一个新功能,并且会定期提交push--主要是为了备份或将我正在开发的内容转移到另一台机器上... 查看详情
git合并某分支某次commit到另一个分支
参考技术A将新分支dev2的一次提交,合并到dev1中1、切到分支dev22、切换分支到dev1上面结束之后,如果不想合并可以用gitcherry-pick--abort放弃本次合并,否则直接push到远程库push到远程git仓库 查看详情
git将某分支的某次提交合并到另一分支
...候,有时需要把某分支(比如develop分支)的某一次提交合并到另一分支(比如master分支),这就需要用到gitcherry-pick命令。1. 首先,切换到develop分支,敲gitlog命令,查找需要合并的commit记录,比如commitID:7fcb3defff2.然后,切... 查看详情
如何将本地未提交的更改合并到另一个 Git 分支?
】如何将本地未提交的更改合并到另一个Git分支?【英文标题】:HowdoImergemylocaluncommittedchangesintoanotherGitbranch?【发布时间】:2010-10-0802:25:15【问题描述】:如何在Git中执行以下操作?我当前的分支是branch1,我做了一些本地更改... 查看详情