My git commands cheatsheet

Basic commands

Configure the author name and email address to be used with commits
      
git config --global user.name "Roland dela Pena"
git config --global user.email [email protected]
      
    
Initialize a new local repository
      
git init
      
    
Check out a local repository
      
git clone /path
      
    
Check out a remote repository
      
git clone username@host:/path
      
    
Include file(s) to staging
      
git add filename1.php filename2.php filename3.php
      
    
Commit all staged and not staged files added/modified
      
git commit -am "Commit message"
      
    
Push to the master branch of remote repository:
      
git push origin master
      
    
Show all staged, not staged or untracked files
      
git status
      
    
Fetch and merge changes on the remote repository to local repository
      
git pull
      
    

... or

      
git fetch
git merge origin/master
      
    
Move file(s)
      
git mv foldersource foldertarget
      
    

... or

      
git add -A
      
    
Change comment message of last commit
      
git comit --amend
      
    
View commits history
      
git log --oneline
      
    

Undoing change commands

Undo the modifications made in one unstaged file permanently
      
git checkout filename.php
      
    
Undo the modifications made in all unstaged files permanently
      
git reset --hard HEAD
      
    
Unstage the modified staged files
      
git reset HEAD
      
    
Delete all untracked files permanently
      
git clean -f
      
    
Undo single commit (without destroying any of the history)
      
git revert HEAD
      
    
Undo any commits (rewrite history and destroy some commits made)
      
git reset --hard be32f3a
      
    

Note: be32f3a is a git short hash.

Branch commands

Create new branch named "dev"
      
git checkout -b dev
      
    
Create new branch named "dev" and pull from master branch
      
git checkout -b dev master
      
    
List all branch
      
git branch -a
      
    
Switch to "dev" branch
      
git checkout dev
      
    
Merge dev branch to master branch (with Fast Forward)
      
git checkout master
git merge dev
      
    
Merge dev branch to master branch (without Fast Forward)
      
git checkout master
git merge --no-ff dev
      
    
Undo merge branch
      
git checkout master
git reset --hard be32f3a
      
    

Where be32f3a is a git short hash referenced before the merge.

Save "dev" branch to remote repository
      
git push origin dev
      
    
Track "dev" branch from remote repository
      
git checkout --track origin/dev
      
    
Setup local "dev" branch to track remote "dev" branch pull down changes from remote repository
      
git branch --set-upstream dev origin/dev
      
    
Delete local "dev" branch
      
git branch -d dev
      
    
Delete a remote "dev" branch
      
git push origin :dev
      
    

Stash commands

Save stash
      
git stash save "Stash message"
      
    
Apply stash to current branch
      
git stash apply stash@{0}
      
    
View all stash
      
git stash list
      
    
Remove a stash
      
git stash drop stash@{0}
      
    

Tag commands

Create tag named "1.0"
      
git tag 1.0 -am "Tag message"
      
    
Save tag "1.0" to remote repository
      
Git push origin 1.0
      
    
View all tags
      
git tag -l
      
    
Undo a commit using created tag "1.0" (rewrite history and destroy some commits made)
      
git reset --hard 1.0
      
    
Change tag name from 1.x-alpha to 1.x-alpha1
      
git tag 1.x-alpha1 1.x-alpha
git tag -d 1.x-alpha
      
    
Tags

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.