Commit your changes

Last update: 16 Aug 2024 [History] [Edit]

Whenever you make significant functional changes to your project, it is very useful to save your progress using git.

tip You can call the git commands from any place within your git repository (the top level directory contains a hidden .git directory). In this case, the repository is MyAnalysis. Note that the path to the file(s) you in your git commands depends on where in the repo you are.

To view which files have been changed since your last commit, use the command:

git status

This will show you a list of all of the files that have been modified or deleted as well as files that are not yet known to the repository.

tip If you see a large number of files that you do not want to commit to the repository, such as temporary files, binary files, or log files, you can create a file called .gitignore in which you define file names or patterns with wildcards (*) that will be ignored by git. There is already a .gitignore file in MyAnalysis with an example of a wildcard pattern to ignore temporary vim files. Commit .gitignore for your changes to take effect.

It is generally good practice to check the changes to a file before committing it. Do this with the command:

git diff <filename>

<filename> should include the relative path to the file from the current directory.

When you have confirmed that the changes in the file are good, stage the file to be committed using:

git add <filename>

You can also remove a file from the repository or commit the deletion of a file using:

git rm <filename>

After staging files, you can double check the changes you have staged to commit using:

git diff --staged

Next, commit the staged changes using:

git commit

The git commit command will open a text editor for you to add a commit message describing the changes. This is an important step so you can keep track of the changes you make.

tip By default, git will use your default editor as defined by the environment variables VISUAL or EDITOR. If neither is defined, it will use vim. You can change the editor for git using, e.g., git config --global core.editor nano.

Finally, push your changes to the remote repository using:

git push

tip You may need to specify the remote repository and the remote branch if you are working on a new local branch or want to push the changes to a different remote repository than the one you cloned from. This is done with git push <remote-name> <branch-name>.