@@ -30,6 +30,7 @@ type CheckoutOptions struct {
3030
3131 SelectorArg string
3232 RecurseSubmodules bool
33+ Force bool
3334}
3435
3536func NewCmdCheckout (f * cmdutil.Factory , runF func (* CheckoutOptions ) error ) * cobra.Command {
@@ -61,6 +62,7 @@ func NewCmdCheckout(f *cmdutil.Factory, runF func(*CheckoutOptions) error) *cobr
6162 }
6263
6364 cmd .Flags ().BoolVarP (& opts .RecurseSubmodules , "recurse-submodules" , "" , false , "Update all active submodules (recursively)" )
65+ cmd .Flags ().BoolVarP (& opts .Force , "force" , "f" , false , "Force merge into local branch" )
6466
6567 return cmd
6668}
@@ -116,7 +118,12 @@ func checkoutRun(opts *CheckoutOptions) error {
116118 // local branch already exists
117119 if _ , err := git .ShowRefs ("refs/heads/" + newBranchName ); err == nil {
118120 cmdQueue = append (cmdQueue , []string {"git" , "checkout" , newBranchName })
119- cmdQueue = append (cmdQueue , []string {"git" , "merge" , "--ff-only" , fmt .Sprintf ("refs/remotes/%s" , remoteBranch )})
121+ // If forced reset to remote
122+ if opts .Force {
123+ cmdQueue = append (cmdQueue , []string {"git" , "reset" , "--hard" , fmt .Sprintf ("refs/remotes/%s" , remoteBranch )})
124+ } else {
125+ cmdQueue = append (cmdQueue , []string {"git" , "merge" , "--ff-only" , fmt .Sprintf ("refs/remotes/%s" , remoteBranch )})
126+ }
120127 } else {
121128 cmdQueue = append (cmdQueue , []string {"git" , "checkout" , "-b" , newBranchName , "--no-track" , remoteBranch })
122129 cmdQueue = append (cmdQueue , []string {"git" , "config" , fmt .Sprintf ("branch.%s.remote" , newBranchName ), headRemote .Name })
@@ -139,11 +146,24 @@ func checkoutRun(opts *CheckoutOptions) error {
139146 ref := fmt .Sprintf ("refs/pull/%d/head" , pr .Number )
140147 if newBranchName == currentBranch {
141148 // PR head matches currently checked out branch
149+
142150 cmdQueue = append (cmdQueue , []string {"git" , "fetch" , baseURLOrName , ref })
143- cmdQueue = append (cmdQueue , []string {"git" , "merge" , "--ff-only" , "FETCH_HEAD" })
151+
152+ // If forced reset to remote
153+ if opts .Force {
154+ cmdQueue = append (cmdQueue , []string {"git" , "reset" , "--hard" , "FETCH_HEAD" })
155+ } else {
156+ cmdQueue = append (cmdQueue , []string {"git" , "merge" , "--ff-only" , "FETCH_HEAD" })
157+ }
144158 } else {
145159 // create a new branch
146- cmdQueue = append (cmdQueue , []string {"git" , "fetch" , baseURLOrName , fmt .Sprintf ("%s:%s" , ref , newBranchName )})
160+
161+ // If forced reset to remote
162+ if opts .Force {
163+ cmdQueue = append (cmdQueue , []string {"git" , "fetch" , baseURLOrName , fmt .Sprintf ("%s:%s" , ref , newBranchName ), "--force" })
164+ } else {
165+ cmdQueue = append (cmdQueue , []string {"git" , "fetch" , baseURLOrName , fmt .Sprintf ("%s:%s" , ref , newBranchName )})
166+ }
147167 cmdQueue = append (cmdQueue , []string {"git" , "checkout" , newBranchName })
148168 }
149169
0 commit comments