Did you know that we can write scripting functions in our shell (zsh/bash) configuration file?
Let's code a simple function that will run the git commands necessary to:
- Commit any changes you've made in the current local repo that you're in (in Terminal).
- Pull updates from the
upstream
remote. - Push the new commits to the
origin
remote (typically your fork).
Depending upon your system, there can be a few possible files in your home folder (~
) used to configure your shell and/or system settings.
Start by listing all of the files in the home folder (including hidden files):
cd ~
ls -a
The shell config file will always be a hidden file that starts with a period/dot character (.
).
Most commonly on MacOS, zsh
is the shell and uses a .zshrc
config file.
Note: If you've installed oh-my-zsh, there are some other config files as well, but the
.zshrc
file can still be used.
Look for any of these files:
.bashrc
.zshrc
.profile
Once you've identified your config file, open it in VS Code:
code <your config file - don't forget the leading period/dot>
Now, let's copy/paste the following at the bottom of the file:
update()
{
git add -A
git commit -m "Add my work"
git pull upstream main
git push origin main
}
Note: The name of
update
is what you will type in Terminal to execute the function and can be changed if you wish.
As you can see, the function simply runs the git commands that you would type manually.
Save the Config File and quit Terminal
Now, whenever you wish to pull changes from your instructor's upstream, you can simply run update
in Terminal while somewhere within the repo, e.g., the "Lectures" repo.