This article is for anyone utilizing a git repository (GitHub or CodeCommit) and provides information on practices employed at St. Thomas. If you have not already set up git on your computer, we recommend following our
developer environment documentation available for Windows, Mac, Linux, and virtual environments (UTM or WSL).
By understanding how Git works, projects can be easily maintained, shared, and even integrated into automated deployment pipelines.
What is Git?
- Git is a tool that helps you keep track of changes in your code, work with others, and go back to earlier versions if something breaks. Think of it as a “save point” system for your projects. Every time you commit, you create a checkpoint you can return to later.
- Git is called “distributed” because every developer has a full copy of the project history on their own computer.
- A git repository can store scripts, application code, documentation, code examples, and more.
- At St. Thomas we utilize GitHub and AWS CodeCommit repositories depending on the intended use case, audience, and deployment methods.
10 Things to Get Started with Git:
1. Configure User Info:
Before you can clone, pull, or push code to and from a remote repository stored on GitHub or CodeCommit, you need to set up your git configuration. This is outlined in the St. Thomas Development Environment Configuration documentation.
The most common issue regarding Forbidden Access, Not Found, Missing Credentials, etc., can be solved by reviewing the Development Environment Configuration. When using St. Thomas repositories in AWS or GitHub, please, please, please ensure you have the configuration set-up correctly! It will save you from a lot of headaches!
Before making commits, set your name and email so your work is properly credited:
- St. Thomas Development Environment: set directly in
.gitconfig file as specified in the Development Environment Configuration documentation.
- If you are not using St. Thomas organization accounts and require just a simple set-up, you can set your git configuration from the command line.
2. Clone or Initialize a Repository:
While you can connect an initialized, local git project to a remote repository later, the preferred method at St. Thomas is to create the repository in CodeCommit or GitHub first, and then clone the empty repository.
When creating repositories in St. Thomas GitHub or CodeCommit accounts, please follow proper structure, categorization, and tagging policies to keep it organized.
An initialized or cloned repository creates a hidden .git folder that stores all your version history.
3. Staging and Committing Changes:
Note: Adding and Committing only creates the checkpoint on your local machine. Changes are not actually saved to the remote repository for others to see until you perform what is called a "push"
Which brings us to...
4. Pushing and Pulling Changes:
After you add and commit changes, you will need to "push" your work to the remote repository for others to see (and for you to pull later if you move to a different machine). Pushing your changes ensures you don't loose your work if something happens to your machine!
It is always good practice to pull changes regularly, at the start of the work day, when you move to a different computer, or when working in repositories with frequent changes.
Push and Pull regularly to ensure you don't loose any work and that you have the latest changes!
- After one or more commits, push your changes to the remote repository:
git push
- At the start of your work day, and at frequent intervals on busy repositories, pull changes:
git pull
5. Branching for Environments:
6. Merging Branches:
7. Undoing Mistakes:
- Git makes it easy to fix mistakes:
- For more advanced fixes, see the “More Useful Commands” section in the docs.
8. Viewing History and Status:
9. .gitignore:
- Use a
.gitignore file to tell Git which files or folders to ignore (not track). This is useful for things like node_modules/, build folders, or secret files.
- Example
.gitignore:
# Ignore anything starting with dot (common)
.*
# Ignore zip and log file extensions
*.zip
*.log
# Ignore directories
node_modules/
dist/
build/
# DON'T Ignore these
!.gitignore
!.nvmrc
- If you already committed a file, remove it from Git but keep it locally:
git rm --cached filename.txt
10. Diagramming Branches in Markdown: