Useful Git Command Line Commands and Examples

Useful Git Command Line Commands and Examples
By Shay Anderson on October 2013
Most the times git can be used through a GUI for easy access, however, sometimes it is required to use a CLI (or commands through a command line) to work with git. I’ve built the following list of useful git commands.

Configuration Settings
Setup your name for git: git config –global user.name [your name]And email: git config –global user.email [your email address]

Create and Checkout
To create a new git local repository use (inside the local directory): git init
You can create a working copy of a local git repository with: git clone [path to repo]Or, if you’re using a remote repo use: git clone [user]@[host]:[path to repo]

Add File(s)
Add files with git add .For all files, or for single use: git add [file]

Status, Show and Log
Get git status for working directory using: git statusOr, for particular file: git status [file] And for more details about a branch use: git show [branch ID]View the log with: git log

Commit
Commit using: git commit -m “my commit message”Or to add/commit all use -a option: git -am “my message”

Other Useful Commands
Here is a list of other useful git commands:

Find differences: git diff [file (optional)]
Completely remove file: git rm [file]
Move / rename: git mv [source] [target]
Revert to a repository file after changing locally: git checkout — [file]
Modify existing message (last commit message) and/or add files to last commit: git commit –amend -m “new message”
Revert to file from an older commit: git checkout [hash] — [file]
Revert all changes from preview change: git revert [hash]
Revert all back to revision x, gets rid of any changes in working directory: git reset –(soft|mixed|hard) [hash]
Test what files to remove from working directory that you no longer want/need: git clean -n
Remove files from working directory: git clean -f
Print log using only one line per entry: git log –oneline
List branches (asterisk means current branch): git branch
Show remote branches: git branch -r
Show local and remote branches: git branch -a
Create new branch: git branch mybranch
Checkout branch: git checkout [branch name]
Create branch and checkout: git checkout -b [branch name]
Move/rename branch: git branch -m [orignal branch] [new branch name]
Delete branch: git branch -d [branch name]

git merge [branch name] (merge branches, with working directory branch)
git merge [remote name/branch name] (merge remote branches)
git stash save “message here” (save stash)
git stash list (show stashes)
git stash show stash@{n} (n is stash ID int, display stash info)
git stash pop stash@{n} (pull stash data and remove)
git stash apply stash@{n} (apply stash data, leave stash data in stash)
git stash drop stash@{n} (delete stash data)
git stash clear (drop all stash data)
git remote (display remotes)
git remove -v (display details)
git remote add [alias] [url] (add remote)
git push -u [alias] [branch ex: git push -u origin master (-u adds tracking to branch)
git push (simple after tracking added)
git push [alias] :[branch] (delete branch from remote with ‘:’), ex: git push origin :my_branch
–OR–
git push [alias] –delete [branch]
git fetch [alias] (sync with remote), ex: git fetch origin
git fetch (simple if tracking one remote)
git pull (fetch + merge)
git clone [url] [dir name(optional)]
git help [command] ex: git help commit