Subject: [PATCH] subtree: Teach push the --force option

From: Joshua Watt <[email-masked]>
Date: Wed, 30 Jan 2019 12:50:47 -0600

Force pushing to subtree remotes can occasionally be a useful workflow,
particularly when the remote uses a workflow like GitHub pull requests.

Signed-off-by: Joshua Watt <[email-masked]>
---
 contrib/subtree/git-subtree.sh  | 14 +++++++++++++-
 contrib/subtree/git-subtree.txt |  9 +++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 147201dc6c..39792198b4 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -29,6 +29,8 @@ onto=         try connecting new tree to an existing one
 rejoin        merge the new branch back into HEAD
  options for 'add', 'merge', and 'pull'
 squash        merge subtree changes as a single commit
+ options for 'push'
+f,force       force push
 "
 eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
 
@@ -48,6 +50,7 @@ annotate=
 squash=
 message=
 prefix=
+force=
 
 debug () {
 	if test -n "$debug"
@@ -137,6 +140,9 @@ do
 	--no-squash)
 		squash=
 		;;
+	-f)
+		force=1
+		;;
 	--)
 		break
 		;;
@@ -892,7 +898,13 @@ cmd_push () {
 		refspec=$2
 		echo "git push using: " "$repository" "$refspec"
 		localrev=$(git subtree split --prefix="$prefix") || die
-		git push "$repository" "$localrev":"refs/heads/$refspec"
+		if test -n "$force"
+		then
+			fopt="--force"
+		else
+			fopt=""
+		fi
+		git push $fopt "$repository" "$localrev":"refs/heads/$refspec"
 	else
 		die "'$dir' must already exist. Try 'git subtree add'."
 	fi
diff --git a/contrib/subtree/git-subtree.txt b/contrib/subtree/git-subtree.txt
index 352deda69d..e956e6ab7d 100644
--- a/contrib/subtree/git-subtree.txt
+++ b/contrib/subtree/git-subtree.txt
@@ -175,6 +175,15 @@ remain intact and can be later split and send upstream to the
 subproject.

+OPTIONS FOR push
+----------------
+-f::
+--force::
+	This option is only valid for the push command
++
+Force push to the remote branch, just like gitlink:git-push[1]
+
+
 OPTIONS FOR split
 -----------------
 --annotate=<annotation>::
-- 
2.20.1