Git: Useful Commands

Below are a few most common commands that are useful to know:

  • Clean up your branch list of branches that doesn’t exist any longer on the remote repository: 
    • git fetch --prune
  • Delete local branch in local repository:
    • git branch -D <branch_name>
  • Delete remote branch in remote repository:
    • git push origin --delete <branch_name>
  • Show which files are tracked or not:
    • git status
  • Track all changes you have done so far:
    • git add .
      
      
  • Commit your tracked changes to your local repository:
    • git commit
  • Revert addition done in git:
    • git reset
    • git reset <filename>
    • git rm --cached <filename>
  • Submit your tracked changes, saved in your local repository, to the remote repository:
    • git push -u origin <branch_name>
  • Merge one branch into your current branch:
    • git merge --no-ff <other_branch>
  • Change branches: 
    • git checkout <branch_name>
  • Create a new branch from current branch:
    • git checkout -b <new_branch_name>
      
      
  • Revert file to its state in master branch:
    • git checkout origin/master <filename>
  • Find and Restore a deleted file from repository:
    (First we find if the file exist then we retrieve it)

    1. git rev-list -n 1 HEAD -- <FILE_PATH/FILENAME>
    2. git checkout <branch_name>^ -- <FILE_PATH/FILENAME>
  • Save in the stash your files changes (add -u or –include-untracked if needed):
    • git stash
      git stash --include-untracked
      git stash -u
  • Apply your file changes saved in the stash into your current branch:
    • git stash pop 

      or

    • git stash apply
  • History of an specific file that you don’t know where reside:
    • git log --all "**/file.groovy"
  • Stash untracked files without staging them (Version 1.7.7+):
    • git stash save -u
  • Get all the stuff from remote repository:
    • git pull
  • Problems when pulling? Asking you to retry a link issue? Try the garbage collector
    • git gc
  • Reset local branch to origin (remote) version
    • git reset --hard origin/<branch_name>
  • Change Local and Remove branch name 
    • git branch -m <branch_new_name>
      • git branch -m <branch_old_name> <branch_new_name>
    • git push origin :<branch_old_name> <branch_new_name>
    • git push origin -u <branch_new_name>
Share
Leave a comment