Initialising Git, adding files and committing

Git - Quick Reference

3 min read

Published Jun 19 2025


7
0
0
0

CLIGit

1. Initialising Git on the project

Go to the root folder in the project and run this command to initialise Git:

git init

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:

git init -b branch-name

Don't edit the contents of the .git directory directly.




2. Mark the files for staging

Add a single file to Git:

git add filename

Add multiple, specified files to Git:

git add filename1 filename2 filename3

Add all files that have been added or changed in the current project:

git add -A

Alternatively 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 directories
node_modules/

# OS-specific files
.DS_Store
Thumbs.db
.env


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 and Tumbs.db on Windows.
  • Dependancy folders - such as node_modules, vendor, bin/

Once created, save and commit it:

git add .gitignore
git 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 filename

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:

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.

  1. Create a new repository on your chosen repository, eg. GitHub.
  2. Give the repository a name.
  3. Untick the initialise the repository with a README to avoid conflicts.
  4. Get the remote url that is generated .

Add the remote repository:

git remote add origin https://github.com/your-username/my-project.git

Verify its linked the remote repository:

git remote -v

Push your local main branch to the GitHub main branch:

git push -u origin main

The -u sets the upstream and links main to origin/main.


If the projects already linked to the remote repository then just run:

git push


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact