Git commands which we use on a daily basis

Create a new branch

ShahNilay
1 min readMar 22, 2023
  • git checkout origin/master
  • git fetch — all
  • git pull origin master or git reset — hard origin master
  • git checkout -b branch_name (branch-type/branch-name) (note: branch naming convention can be helpful for better readability)

Add a new commit

  • git add .
  • git commit -m “feat(feature-name): commit message”
  • git push

Squash commits

  • git stash or git reset — hard (remove all local changes)
  • git rebase -i HEAD~number_of_commits (git rebase -i HEAD~3)
  • picking and squashing via terminal (replace pick with s for commit ids which you want to squash)
  • remove/comment out squashed commits commit message
  • exit from terminal
  • git push — force origin feature/branch-name

Reorder commits (master — x — y — z → master y — x — z)

Reordering of commits

ddp for move down

ddkp for move up

Or you can use reset and cherry-pick

  • git log
  • note down commit ids of all 3 commits
  • git reset — hard HEAD~3
  • git log will show your head at master and x,y and z (associated changes) will be removed from the list
  • git cherry-pick y
  • git cherry-pick x
  • git cherry-pick z
  • git push — force origin feature/branch-name

--

--

ShahNilay
ShahNilay

No responses yet