Ads Top

Git basic commands - the web stop

Git is the most widely used version control system in the world. It is free and open source project which tracks changes in the working files of a user and coordinate changes in those working files amongst multiple users.

The most important and basic commands for Git are:

Set up and initialization

git init
initialize an existing directory as a Git repository/create a new git repository.

git clone [url]
creates a working copy of an existing repo.

Staging and Snapshot

git workflow - the web stop

git status
displays the state of the working directory staged for your next commit.

git add [filename]
moves the changes from the actual directory to the staging area.

git reset [filename]
removes the specified file from the staging area but leaves the working directory unchanged.

git diff [filename]
difference of what is changed but not staged.

git diff --staged
difference of what is staged but not committed.

git commit -m "commit message"
takes the staged snapshot and commits it to the HEAD.

git commit --amend
lets you amend the most recent commit.

Branching and Merging

git branching - the web stop

git branch
lists all your branches. A (*) will appear next to the name of your currently active branch.

git checkout [branch-name]
navigates to the specified branch and updates the working directory to match.

git branch [new-branch-name]
git checkout [new-branch-name]
creates a new branch and switch to it at the same time.

or short hand for above code is:

git checkout -b [new-branch-name]
creates a new branch and switch to it at the same time.

git branch -d [branch-name]
deletes the specified branch. It is a safer option in which Git prevents you from deleting the branch if it has unmerged changes.

git branch -D [branch-name]
force deletes the specified branch even if it has unmerged changes.

git branch -m [new-branch-name]
renames the current branch to the new specified name.

git merge [branch]
merges the specified branch's history into the currently active branch.

git log
shows all the commits in the current branch's history.

git log [branch-b]..[branch-a]
shows the commits on branch-a that are not on branch-b.

git diff [branch-b]...[branch-a]
shows the difference of what is in branch-a that is not in branch-b

Sharing and updating

master vs origin/master - the web stop

git push origin [branch]
makes your branch available to others by pushing your branch to remote repository.

git pull
fetches and merges any commits from the tracking remote branch.

git fetch
fetches any commits from the tracking remote branch but does not automatically merge them. So, the local stays the same.

git reset --hard [commit]
clears the staging area, rewrite working tree from the specified commit.

git reflog show
shows a record of your references. It displays each position of HEAD in the last 30 days(by default).

Stashing - Temporary Commits

git stash
takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.

git stash list
lists stack-order of stashed file changes.

git stash pop
writes working from top of stash stack.

git stash drop
discards the changes from top of stash stack.

git stash apply
applies the changes from the top of stash stack.


  1. very informative post for me as I am always looking for new content that can help me and my knowledge grow better.

  2. Git is a version control system (VCS) created for a single task: managing changes to your files. It lets you track every change a software project goes through, as well as where those changes came from. This makes Git an essential tool for managing large projects, but it can also open up a vast array of possibilities for your personal workflow.


Powered by Blogger.