Initialising Git, adding files and committing
Git - Quick Reference
3 min read
Published Jun 19 2025, updated Aug 17 2026
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:
git initThis 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:
git init -b branch-nameDon't edit the contents of the .git directory directly.
2. Mark the files for staging
Add a single file to Git:
git add filenameAdd multiple, specified files to Git:
git add filename1 filename2 filename3Add all files that have been added or changed in the current project:
git add -AAlternatively you can run this to add all files:
git add .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:
# lines are comments# All .log files*.log# Specific directoriesnode_modules/# OS-specific files.DS_StoreThumbs.db.envYou want to avoid committing:
- Temporary files - such as build fines and logs.
- Secrets - such as
.envfiles and API keys. - System files - such as
.DS_Storeon macOS andTumbs.dbon Windows. - Dependancy folders - such as
node_modules,vendor,bin/
Once created, save and commit it:
git add .gitignoregit commit -m "Added .gitignore"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:
git rm --cached filenameYou 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:
git commit -m "A commit description to record against the commit"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:
git remote add origin https://github.com/your-username/my-project.gitVerify its linked the remote repository:
git remote -vPush your local main branch to the GitHub main branch:
git push -u origin mainThe -u sets the upstream and links main to origin/main.
If the projects already linked to the remote repository then just run:
git push