pages

Monday, 11 May 2020

Git - common commands

Below are some common git commands which help with almost anything you need to do with git day to day basis.


How to get the latest changes

  • git fetch: gets the latest changes from server, keep it in local without applying.
  • git pull: gets the latest changes from server, try to merge with your local to make sure your local is up-to date with the server. 
  • git rebase : rebase your working directory with the latest changes on the given branch 
  • git reset: reset all your local changes and moves you to latest head position available in local (if you have done git fetch before, this will be same position as server) Flags are listed below 
    • hard : lose all uncommited changes and all untracked files 
    • keep : It only resets the files which are different between the HEAD and local. It aborts the reset if one or more of these files has uncommited changes. It basically acts as a safer version of hard. 
    • soft 
    • mixed 
  • Head~1 : points you to one commit before head, used with git reset.


Save your changes 

  • git stash: saves your changes in local. 
    • m : used to pass a message to stash (git stash -m "stash message") 
  • git stash pop: gets the latest stash and merge with your code. 
  • git commit: commits your changes to working directory. 
    • a : all 
    • m : used to pass a message to commit (git commit -m "commit message") 
  • git push: sends all the commits from working directory to server. 
    • u : upstream, pushes the local branch to server 

Miscellaneous 

  • git status : tell you about your working tree/directory 
  • git add : add the files to stage, ready for commit
    •  . : adds all the files 
  • git branch : tells about all the branches available 
  • git branch : creates a branch with that name 
  • git checkout : checkout the branch specified