Skip to content

Instantly share code, notes, and snippets.

@jim-clark
Last active August 15, 2024 11:20
Show Gist options
  • Save jim-clark/d9d2f29c1594e7d0f4ed1e06155cc527 to your computer and use it in GitHub Desktop.
Save jim-clark/d9d2f29c1594e7d0f4ed1e06155cc527 to your computer and use it in GitHub Desktop.

Automate Pulling Updates From Upstream

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:

  1. Commit any changes you've made in the current local repo that you're in (in Terminal).
  2. Pull updates from the upstream remote.
  3. Push the new commits to the origin remote (typically your fork).

Identify the Shell Config File

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 (.).

MacOS

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.

Windows WSL

Look for any of these files:

  • .bashrc
  • .zshrc
  • .profile

Edit the Shell Config File in VS Code

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

Using the Function

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.

References

Bash Scripting Cheatsheet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment