Initialising Git, adding files and committing
Git - Quick Reference
3 min read
Published Jun 19 2025
Guide Sections
Guide Comments
1. Initialising Git on the project
Go to the root folder in the project and run this command to initialise Git:
This creates a hidden .git
directory in the root of the project folder where it stores all of its data and config.
This creates a branch using the name set in the config setting init.defaultBranch
which by default is usually main
, if you want to override this and use a specific branch name then you can run:
Don't edit the contents of the .git
directory directly.
2. Mark the files for staging
Add a single file to Git:
Add multiple, specified files to Git:
Add all files that have been added or changed in the current project:
Alternatively you can run this to add all files:
Note git add .
only deletes files that are in the current directory, where as git add -A
will delete files in the whole project. They both add all new files and edited files.
You can add a text file called .gitignore
to a projects root folder, to specify any files that you don't want including when adding files to be staged.
An example of a .gitignore
file:
You want to avoid committing:
- Temporary files - such as build fines and logs.
- Secrets - such as
.env
files and API keys. - System files - such as
.DS_Store
on macOS andTumbs.db
on Windows. - Dependancy folders - such as
node_modules
,vendor
,bin/
Once created, save and commit it:
If a file is already tracked (ie. committed), then adding it to the .gitignore
file won't stop Git from tracking it. You would need to remove it first:
You can also have nested .gitignore
files in subdirectories too and Git will respect them.
3. Create a snapshot of the staged files
Once all changes are added and staged, you then need to commit these changes to the local branch:
Add a descriptive message to commits to easily see in the history what the changes are.
4. Send the commits to the remote repository
If this is a new project that you haven't linked to a remote repository yet, then first you need to do that.
- Create a new repository on your chosen repository, eg. GitHub.
- Give the repository a name.
- Untick the initialise the repository with a README to avoid conflicts.
- Get the remote url that is generated .
Add the remote repository:
Verify its linked the remote repository:
Push your local main branch to the GitHub main branch:
The -u
sets the upstream and links main
to origin/main
.
If the projects already linked to the remote repository then just run: