Some usefull GIT commands

Sync up/Reset a branch (master) of fork from the upstream (LOST ALL LOCAL CHANGES)

  • git remote add upstream url_to_original_repo
  • git fetch upstream
  • git checkout master
  • git reset --hard upstream/master
  • git push origin master --force

Fast switch to previous branch

  • git checkout -

Create a new branch from remote branch

  • git checkout -b the_local_branch origin/the_remote_branch
  • git checkout -b the_local_branch upstream/the_remote_branch

Delete a local branch

  • git branch -d the_local_branch
  • git branch -D the_local_branch

Remove a remote branch

  • git push origin :the_remote_branch

Remove stale branches at local

  • git remote prune origin

Get code at a specifc PR (GitHub) for test or compare code

  • git fetch origin pull/ID/head:BRANCHNAME
  • git fetch upstream pull/ID/head:BRANCHNAME

Note: Can't use git pull to get latest with upstream. Need to delete and create branch again to get latest update.

Reset changes that are not staging/commit in branch

  • git checkout .

Updated 2017-07-20

Registration Login
Sign in with social account
Lost your Password?
Registration Login
Sign in with social account
A password will be send on your post
Registration Login