Initialising Git, adding files and committing

Git - Quick Reference

3 min read

Published Jun 19 2025, updated Aug 17 2026


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 directoriesnode_modules/# OS-specific files.DS_StoreThumbs.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 .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 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

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
Git - Quick Reference | Initialising Git, adding files and committing | SimpleSteps.guide