0

Git FastPatch Shell Script — Donat Studios

 2 years ago
source link: https://donatstudios.com/FastPatch
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Fast Patches with Git

In my work, I deal with a lot of very similar codebases - and often if I fix something in one project I'll want to fix it in many others. For a long time this has meant popping open Beyond Compare, which works, but isn't the simplest solution.

I use git on my projects - but they're not similar enough just to be branches. Recently I came up with a way to patch one or more commits from a project to another easily. Here is a simple shell script I wrote to handle the task.

Simply put, you can use it either by

$ fast-patch.sh /z/my_project

to patch a project with the latest commit from the project in the working directory or with an optional parameter, specify a number of commits, eg

$ fast-patch.sh /z/my_project 5

Also, it will generate a patch log to ~/fastPatch.log which I generally use directly for my commit message by way of

$ git commit -F ~/fastPatch.log

Any comments on how I could improve it, or questions are quite welcome.

#!/bin/sh set -o errexit

# make sure we're at the root of git repo if [ ! -d .git ]; then echo "Error: must run this script from the root of a git repository" exit 1 fi

if [ ! -d "$1/.git" ]; then echo "Error: Paramater 1 must be host to a git repository" exit 1 fi

if [ $# -eq 2 ]; then commits=$2 else commits=1 fi

#Patch Log echo "Patched From: " > ~/fastPatch.log pwd >> ~/fastPatch.log git log -n $commits >> ~/fastPatch.log

#Patch git format-patch -$commits --stdout > ~/fastPatch.patch

cd "$1" #git stash git apply --reject --whitespace=nowarn ~/fastPatch.patch #git add . #git commit -F ~/fastPatch.log #git stash pop

echo "Patched With:" cat ~/fastPatch.log



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK