3.2 React Tooling

Git CLI Reference

Main not Master to be the new default branch name

As of October 1, 2020, all new repositories created on GitHub will have the name of the default branch set to main rather than the previous default name of master.

Update to the latest version of git

Please update your git cli (opens new window) to version 2.28 or later.

This version introduced the ability to set the name of the default branch when creating a new repository. You can choose to set it manually using the -b flag e.g.

git init -b main
1

Or, you can set main to be the default in git global config. This will keep you consistent with the new GitHub defaults.

git config --global init.defaultBranch main
1

# Initialize a project

Create a new project folder (e.g. my-awesome-app) and turn it into a git repository.

mkdir my-awesome-app
cd my-awesome-app
git init -b main
1
2
3

If you already have created the git repo but want to change the master branch to main and set it as the default branch, then you can do this:

git branch -M main
1

# .gitignore

The hidden file .gitignore (don't forget the leading period) tells git which files and folders should not be tracked for changes.

At the minimum your .gitingnore file should include:

.DS_Store
node_modules
1
2

# .gitkeep

If you have any empty folders that you want to keep as part of your repo, you can place an empty file inside that folder. The problem is that how do you know if the file is there as part of the project or as a way to save an empty folder.

To address this issue, the convention of calling that empty file .gitkeep has been used.

# README

Every git repo should have a good README.md file to explain what the project is about and examples of how to use it.

echo "# My awesome app" > README.md
1

It obviously needs more detail than that. Check out A Beginners Guide to writing a Kickass README (opens new window) for some advice/inspiration.

# Stage files for commit

Select individual files to be included in the next commit.

git add [filename]
1

Using the -A or --all flags will stage all the adds, modifies, and removes to match the current working tree.

git add -A
1

# Stop tracking changes on a file

Remove a file from the git tracking index, but not from the project folder. This is good for when you have accidentally forgotten to create a .gitignore file and want to stop tracking files. e.g. node_modules

git rm --cached [FILENAME]
1

# Create a commit

Commit a change log for all staged files. Make sure to include a clear and descriptive message using the -m flag. It is a good practice to start most commit messages with an action verb – add, update, fix, remove – followed by the relevant feature or bug description.

git commit -m 'ADD feature x'
1

# Remote Repositories

It is usually the case that we want to link our local repository to some remotely hosted repo on GitHub, GitLab or BitBucket. If you have started your project by cloning a remote repository, then that link is already set. If not, you can add it with this command:

git remote add origin [URL]
1

You can list the currently linked remote repo(s) with

git remote -v
1

# Sync your local commits to the remote

origin is the default name for the remote repo. main is the branch that you want to push. Git will try to push to a branch with the same name on the remote. These can be updated to match your specific project settings.

git push origin main
1

If you need to push to a different branch name on the remote, you can use this syntax localBranch:remoteBranch.

git push origin main:gh_pages
1

# Help

Get quick reminders with git help on the command line. For more detailed explanations, see the full git documentation (opens new window) website. There is also a great cheat sheet at the Atlassian git tutorial (opens new window) site.

Steve's Learning Git playlist (opens new window)

Steve's Learning Github playlist (opens new window)

Last Updated: : 9/6/2021, 8:57:42 AM