This script provides a custom Git command git prev-branch [steps]
, allowing you to check out a previously used Git branch by specifying how many steps back in the branch history you wish to go.
- Save
git-prev-branch.js
to a directory in your PATH (e.g.~/git-prev-branch.js
). - Make the script executable:
chmod +x git-prev-branch.js
- Rename the script to remove the
.js
extension:mv git-prev-branch.js git-prev-branch
To use the command, type git prev-branch [steps]
, where [steps]
is the number of steps back in your branch history. For example, git prev-branch 1
will check out the branch that was used just before the current one.
To use this script on Windows, you'll need to have Node.js installed and be able to run Node.js scripts from the command line. Additionally, you will need a way to execute the script as a Git command. Here's how to set it up:
-
Install Node.js: If not already installed, download and install Node.js from nodejs.org.
-
Save the Script: Save the
git-prev-branch.js
file to a directory. -
Add to PATH:
- Right-click on 'This PC' or 'My Computer' on your desktop or in File Explorer.
- Click 'Properties'.
- Click 'Advanced system settings'.
- In the System Properties window, click the 'Environment Variables' button.
- In the Environment Variables window, select the 'Path' variable in the 'System variables' section, and click 'Edit'.
- Add the path to the directory where you saved
git-prev-branch.js
. - Click OK in all dialogs to close them.
-
Create a Batch File for Git Command:
- In the same directory, create a new file named
git-prev-branch.bat
. - Edit the file and add the following line:
@node "%~dp0git-prev-branch.js" %*
- This batch file allows Git to use the Node.js script as a command.
- In the same directory, create a new file named
-
Usage:
- Open Git Bash or your preferred shell.
- Use the command as described previously:
git prev-branch [step]
. - The
[step]
parameter works the same as in the Linux/Mac instructions.
This setup allows Windows users to run the script as a Git command. Ensure that the directory containing both git-prev-branch.js
and git-prev-branch.bat
is in your PATH.