Saturday, June 27, 2015

Useful git commands

Lets have a look at some of the useful git commands 

Get a clone of the remote repository

  • git clone <remote repository link>

Get a pull from the remote branch

  • git pull <remote branch name>

Get a pull from the remote branch and rebase the remote branch with the local branch

  • git pull --rebase origin <branch name>

Get all the branches from the remote repository 

  • git fetch --all

Push the changes to remote

  • git push origin <branch-name>
  • git push origin <branch-name> --force #force push the changes

Checkout a branch

  • git checkout <branch name>

Checkout a new branch from the current branch

  • git checkout -b <branch name>

Add a file to commit

  • git add  <filename> #Adds 1 file
  • git add --all #Adds all the files including tracked and un-tracked files

Commit with a message

  • git commit -m "message name"

Stash the current changes and make branch pristine

  • git stash

Un-stash the last changes which were stashed ( hence make the branch dirty)

  • git stash apply

Delete branch

  • git push :<branchname> # delete remote branch name
  • git branch -d <branchname> #delete local branch name
  • git branch -D<branchname>#force delete local branch name 

Change the last commit message 

  • git commit --amend -m "New commit message"

Change remote branch name 

  • git branch <new-branch-name> <origin old-branch-name> #change remote branch name with the new name
  • git push origin <new-branch-name> #push the new branch with the new name
  • git push origin :<old-branch-name> #delete the old branch branch
  • git push -D <old-branch-name>

Change local branch name

  • git branch -m <old-branch-name> <new-branch-name> #If you want to rename any branch
  • git branch -m <new-branch-name> #If you want to change the current branch

Reset last commit 

  • git reset --hard HEAD~1
  • git push origin <branch-name> --force # This step will push the reverted changes back to the remote

Reset last commit but keep the changes in local branch

  • git reset HEAD~1

Rebase your current branch changes with the local master branch

  • git rebase origin/master

Merge your changes with the master branch

  • git merge <branch-name> --no-ff  #Creates a commit for merge and does the merge with fast-forward mechanism

Get git commit history in a compact way

  • git log oneline

No comments:

Post a Comment