A collection of small scripts to make the git workflow easier
These scripts are intended for making the GitHub workflow easier.
They support a single "central" repo as well as the common dual
"upstream/fork" repo setups. In particular, both the setup recommended
by GitHub's help on configuring remotes and used by GitHub's gh tool
(upstream named upstream
and fork named origin
) and the setup created by
GitHub's hub tool (upstream named origin
and fork named $GIHUB_USER
)
are supported.
Note that nothing prevents using most of these scripts with other hosting services like GitLab.
To install, clone the directory and run the install script:
git clone https://github.com/nicksay/git-scripts.git
cd git-scripts
./install.sh
This will place the scripts in $HOME/bin
.
Since the scripts are intended for making the GitHub workflow easier, the best usage documentation is the lifecycle of a change.
To get started, create a new branch:
git new "my-change"
Then, edit and commit code as normal.
When you're ready, create a pull request for your change:
git hub-pull-request
This will prepare your branch for a pull request by rebasing on the latest commits from upstream, push your branch to your fork, and create a pull request using the gh tool or hub tool if one's installed or open the GitHub compare URL if not.
After your pull request is accepted, you can finish up:
git done
This will make sure both your local and fork repos are updated with the latest changes from upstream and remove branches have been merged or squashed with main, including the one you created for your change.
Create a GitHub pull request.
Calls git prepare
and git push
followed by one of the following:
gh pr create
, hub pull-request
, or opening a compare URL.
Prints the GitHub upstream and fork remotes.
git hub-remotes [--upstream|--fork]
Prints the GitHub user.
git hub-user
Finds the value from the following sources in descending order:
- environment (
$GITHUB_USER
) - .git/config or ~/.gitconfig (
git config --get hub.user
) - ~/.config/gh/hosts.yml (
github.com -> user
) - ~/.config/hub (
github.com -> [0] -> user
)
If a value is not found, prompts for one and saves it to .git/config.
git done
Finishes a branch: returns to main, runs git sync
and git tidy
.
git new <branch>
Creates a new branch from main.
Arguments:
branch
: the name of the new branch to create
git prepare [upstream] [branch]
Prepare your current branch for a pull request.
Arguments:
upstream
: defaults togit hub-remotes --upstream
branch
: defaults to "main"
Fetches the latest commits from [upstream]
and rebases on
[upstream]/[branch]
. Automatically avoids interactive mode unless
needed and will rebase --autosquash
all commits made with
commit --fixup
and commit --squash
without needing to launch an
editor.
Afterward, run git push
to update an existing pull request on GitHub.
git sync [upstream] [fork] [branch]
Keep your local and fork repos synced with upstream.
Arguments:
upstream
: defaults togit hub-remotes --upstream
fork
: defaults togit hub-remotes --fork
branch
: defaults to the current branch
Pulls the latest commits from [upstream]/[branch]
and then pushes
to [fork]/[branch]
. Uses pull --prune
and push --prune
to remove
old branch references.
git tidy [branch]
Deletes branches merged or squashed with a base branch.
Arguments:
branch
: defaults to "main"