Git Basic Commands 101

This post is just a basic listing of simple git commands. These are the basics commands to start using Git, this is mostly for my own
reference.  You can download git here: https://git-scm.com/. For a complete guide on git, check out https://git-scm.com/book/en/v2

Local Repo Git Commands

 

Command

 

 

Description

 

 

Example

 

 

git init

 

 

Used to set up a directory as a Git
Repo

 

 

git init

 

 

git status

 

 

Check
the status of the existing repo branch

 

 

git
status

 

 

git branch
<branch>

 

 

Creates a branch off current
branch

 

 

git branch MyBranch

 

 

git checkout
<branch>

 

 

Switches
your repo to another branch

 

 

git
checkout MyBranch

 

 

git add
<filename>

 

 

Adds a single for to the staging
area

 

 

git add MyFile.cpp

 

 

git add .

 

 

Adds
all files in the current repo branch to staging
area

 

 

git
add .

 

 

Git commit

 

 

Commits a change to the
repo

 

 

git commit

 

 

git commit -m
‘<message>’

 

 

Commits
a change using an inline comment

 

 

git
commit -m ‘Change 3’

 

 

git commit -a -m
‘<message>’

 

 

Stages and Commits all change using inline
comment

 

 

git commit -a -m ‘Change
3’

 

 

touch
<filename>

 

 

Creates
a blank file

 

 

touch
.gitignore

 

 

git stash

 

 

Places all committed changes into a working directory
for later use.

 

 

git stash

 

 

git stash apply

 

 

Recovers
the stash back to the branch

 

 

Git
stash apply

 

 

Git Ignore file can be used to exclude files from the repo. Use pattern or file name in the “.gitignore” file. I.e. *.log.

Remote Repo Git Commands

 

Command

 

 

Description

 

 

Example

 

 

git remote

 

 

Displays the currently available remote
repos

 

 

git remote

 

 

git clone
<url>

 

 

Clones
a remote repo to your local repo

 

 

git
clone https://github.com/account/repo.git

 

 

git remote -v

 

 

Displays the repo URL

 

 

git remote -v

 

 

git fetch
<remoterepo>

 

 

Will
fetch any changes made in the remote repo since the last close or fetch. This
will not perform a merge.

 

 

git
fetch origin

 

 

git pull origin

 

 

Will fetch and merge the changes from a remote repo
into your local

 

 

git pull origin

 

 

git push <remoterepo>
<branch>

 

 

Pushes
changes from the local repo to the remote
repo

 

 

git
push origin master

 

 

git remote add <repoalias>
<cloneurl>

 

 

Creates a new local repo from a remote with an
alias

 

 

git remote add myrepo
https://github.com/account/repo.git

 

 

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s