Common Git Commands

Subscribe to my newsletter and never miss my upcoming articles

Sometimes, I forget some of the most basic Git commands. There are a handful that you use regularly, which is what this post will be for.

A quick reference for Git Commands

Setup

The commands you will use any time you need to install and set up Git, or if you need to change/update the information.

git config --global user.name "name"

Set your username, usually the same as what you use for GitHub/GitLab/BitBucket etc.

git config --global user.email "email"

Set your email address to be associated with each change, again usually the email you used for GitHub/GitLab/BitBucket etc.

Sidenote: quotations are required!

Initialize a repository

We use these to initialize a director or to grab a repository from GitHub(or any other host.)

git init

Initialize the directory you are in as a Git repository.

git clone [url]

"Download" a repository from its host.

Stage and Commit

Here we check the status, stage any files we need to, and commit it to the saved snapshot.

git status

Show modified files in your working directory, and whether they are added for commit or not.

git add [file]

Add a specific file to your next commit.

git add .

Add the entire directory recursively.

Sidenote: Working on large, team-oriented projects, it may be good practice to commit by specific file you are working on, to avoid adding anything that shouldn't be there. That is reversible of course, but why waste time fixing it? Otherwise, if you know for sure the files you staged need to be committed, the "." is a handy shortcut.

Side-sidenote: I have a habit of checking the status, adding any non-staged files, then checking the status again to be sure things went in properly before committing.

If you have an error:

git reset [file]

Will unstage the file and retain your changes on your local directory.

git commit -m "[description of changes]"

Commit your staged files with a description of changes you made

Branching

Branches help to isolate work, keep organized, and prevent pushing potentially breaking changes into the main branch.

git branch

Lists all existing branches. A * shows next to the one you are working in.

git branch [branch-name]

Will create a new branch.

git checkout

Will switch to another branch and check it out into your directory.

git merge [branch]

Will merge the branch's history into your current working branch.

Updating

Add Git URLs, pull from remote, merge remote branches, and push local commits to remote repositories

git remote add [alias] [url]

Adds a URL as an alias (like naming your application.)

git fetch [alias]

Fetch all branches from the remote alias.

git merge [alias]/branch]

Merge remote branch with your local branch to keep it up to date. Highly important when working in teams or open source software.

git push [alias] [branch]

Push local commits to remote repository.

git pull

Fetch and merge any changes/commits from the remote branch.

No Comments Yet