git CLI Step By Step Process

Here we explore the step by step process of using the git` CLI (which you must use when using Anvil) to make code changes to a repository. This guide assumes that you’ve done at least steps 1-3 on the Setup git on Anvil Starter Guide. If you’ve not, do those first then come back to this page.

General Process

In general, the process to make changes to our repository is:

  1. Pull the existing repository using git pull

  2. Make some code changes on our editor to code that is in our local repository (such as with Jupyter Notebook)

  3. Use git add to add untracked files to staging, getting them ready to add to a commit

  4. Use git commit -m "my comment on this commit" to make a new commit including all our changes

  5. Use git push to push our commit(s) to the repo

There are numerous other steps you can take in between to verify your work, which we will denote as (optional) steps in between the primary mandatory steps.

You must do all of these steps in order, otherwise you might cause commit conflicts.

1. Pull Existing Repository

For starters, make sure you are on Anvil and in your cloned repository directory and checked out on the main branch. To do this, in the terminal on Anvil, do:

cd /path/to/my/cloned/repository #change directory to the github repository
git branch -a #look at all the branches on this repository

You should see a list of branches displayed, along with an asterisk next to the one you are currently checked out on. Be sure the asterisked branch is main before going on further. If it is not, you can "checkout" (switch to) that branch by doing:

git checkout main

Now let’s make sure the repository that we have locally is the same as the one that is at the origin. origin is relative depending on where you are pushing and pulling from, but in our case, we are always referencing the repository on github.com, not our locally cloned copy. Origin can be set to whatever you want it to, but typically Git users will set the origin to be the remote repository they originally cloned from.

Now, let’s get all the changes that other users have made to the origin repository to our local repository by using git pull:

git pull

You will see a brief summary of changes, or a notification that the repository is already up to date.

We are now ready to make coding changes.

2. Make Changes To Repository

Any of the files that are in your repository folder are being tracked by Git, including all the subfolders. For instance, you can open up a file in Jupyter Lab, make edits to it and save it. The amount of edits you make are up to you, but the general rule of thumb is that if you can’t explain the changes you made in less than 5-10 words, you might want to split up your changes into multiple separate commits.

2.5 Using Git Status (optional)

You can check to see which files Git noticed you made changes to do by doing

git status

You will notice that there are untracked and tracked files. Untracked just means they haven’t been added to staging yet. Staging is the list of files that will be added to the next commit. This is a good way to double check that the changes you made were noticed by Git (sometimes, if you forget to save a file, the changes might not show up here).

3. Adding Files to Staging

We made changes to our files and we are ready to push them. We now have to add them to staging by doing

git add /path/to/the/file

You can also directly add all the files that are untracked to staging by doing

git add -A

3.5 Modifying Staged Files (optional)

Maybe you made a mistake, and there was a file you accidentally added to staging. You can use the command below to remove it (back to untracked):

git restore --staged /path/to/file/to/remove/from/commit

Once again, double check with git status:

git status

4. Make the Commit

Now we have all our files ready to send to origin, and they are in staging. We’ve confirmed with git status that everything is as it should be. Now let’s create the commit:

git commit -m "My Commit Comment"

Describe the changes you made to the repository in the "My Commit Comment". You must use the "" double quotes, but you can have any message you want in there.

4.5 Undoing Our Commit (optional)

We realized we made a mistake on our previous commit, or the comment wasn’t right, etc. Yet we want to keep the changes we made to the file. The code below will remove the commit we just made, but keep the changes on our local repository:

git reset --soft HEAD~1

5. Push Our Changes

We are ready to send our changes to origin. Go ahead and push:

git push

Congrats! You just sent your changes to the origin for everyone else to see. Now, when they git pull, they will receive your changes to their main branch.

Branching Techniques

Some users of git prefer to use local branches (that is, their own personal branch) to make edits on, push the changes to, then push the changes from their personal branch to the main branch. This can be helpful if you are pushing complex coding changes to live production Github repositories whose changes impact live services by pushing locally first (and thus keeping track of your changes) and testing locally, then making pushes. You can find an example of this style of branching here.

Autocommits (Advanced)

Once you feel like you’ve got the general Git flow down, it can be convienent to write a simple shell script to run through all the steps of pulling, adding, committing and pushing into a shell script. The script below assumes only one branch is used and that you are the only person working on a file in the repository. If you’ve made changes to a file which others have as well, doing git pull like this might overwrite some of the changes you’ve made.

Use with caution! This will pull the repository, add all untracked files, make a commit (with a prompt to insert a commit message) and finally push the commit immediately. Ensure you’ve made all the changes you want to make first, then run this.
Make a file called autocommit.sh that contains these lines of code in your home directory, and each time you want to commit, in the terminal just run sh /path/to/autocommit.sh.
Once done typing your commit message, press Ctrl+O to save, and Ctrl+x to quit the nano prompt.
git pull
git add -A
git commit
git push