This package provides scripts to support managing multiple branches of a git repository.
The scripts support a work style in which you have a separate working copy
(a.k.a. "clone") for each branch that you work on. You never switch
branches within a working copy. I find that this reduces branch confusion.
It replaces mechanisms such as git stash.
Its key principles are:
- Do not run git commands in the master branch, except
git pull. (Or, usemvc pull.) - Do not change branches in any working copy.
That is, never run
git checkout. - To create a new branch, run command
gnborgit-new-branch. It creates a new directory holding a new branch that starts as a copy of the branch from which you rangnb. - To clone/checkout an existing branch, run
gcb. It creates a new directory and checks out the branch there.
The commands are:
git-checkout-branchBRANCHNAME: Checks out the given branch of the repository in a new working copy directory. Run this command from within a working copy; the new directory is a sibling of it. Below is a definition for an aliasgnb.git-new-branchBRANCHNME: Creates and checks out the given branch of the repository in a new working copy directory. Run this command from within a working copy; the new directory is a sibling of it. Below is a definition for an aliasgnb.git-push-toFROMDIR TODIR ...: Pull from FROMDIR into TODIR, compile it, then push TODIR to its remote if compilation succeeded. The two directories should be working copies (that is, git clones). You may also pass a list of directories: each is pushed into the subsequent one.git-pull-fromFROMDIR: Pull from FROMDIR into the current directory, compile it, then push it to its remote if compilation succeeded. The two directories should be working copies (that is, git clones).git-orphaned-branches: Lists directories named*-branch-*that are a working copy for a branch that was deleted in the remote. Typical usage isrm -rf $(git-orphaned-branches)orrmgob(see alias below).compile-project: Runs a Gradle or Maven command to compile the project that contains the current directory. The command-line arguments for the compilation can be customized.
More documentation of each script appears at the top of the script. Click the command names above to see that documentation.
Clone this repository:
git clone https://github.com/mernst/manage-git-branches.gitFor convenience, add the following commands to your shell startup file, such as ~/.profile.
export PATH="/path/to/manage-git-branches:${PATH}"
alias gcb=git-checkout-branch
alias gnb=git-new-branch
alias rmgob='rm -rf $(git-orphaned-branches)'If you use git-checkout-branch and git-new-branch, then you will never
switch the branch for a working copy. That is, you will never run git checkout BRANCHNAME. Instead, you will have a separate working directory for each
branch that you work on. The directory's name will be
REPONAME-branch-BRANCHNAME.
This convention enables you to easily work on multiple branches at a time:
- No confusion about which fork or branch you are on: this information is evident in the directory name.
- No need to switch branches with
git checkout. (Just use file system operations such ascdandln.) - No need to commit or stash changes before switching branches, which would otherwise be necessary to avoid accidentally mixing work between branches.
- Faster builds, because each branch has its own version of built executables, avoiding the need for your build system to recompile everything when switching branches.
- Easier non-git operations: comparing branches using your favorite tool, searching multiple branches, reconciling branches, copying text between them, etc.
- Greater disk usage than if you have a single clone and you switch branches within that clone. Usually, disk space is plentiful and the foregoing advantages are worthwhile. If you have very large repositories and limited disk space, however, this approach may be undesirable.
If other programs need a specific name for your repository, then you may wish to
make the directory REPONAME be a symbolic link to whichever branch you are
working on at the time, such as REPONAME-branch-main when you want to use the
main branch.
The multi-version-control program manages multiple clones, much as this repository manages multiple branches.