Git Commands

Git Rebase Vs Git Merge:

“git rebase” command can be used for many purposese like:

  • To integrate the changes of base branch into the feature branch and reapply the new commits on top of it.
  • To move a commit from one branch to another branch.
  • To remove an existing commit from a branch.
  • To rename the commit message, merging the commits, etc.

While “git merge” is used to merge the 2 branches. Since it does not rewrite the commit history, so it is considered as safe in comparison of “git rebase”


Command to do the commit in the past date:

  • GIT_AUTHOR_DATE=”2019-09-03T14:00:00″ GIT_COMMITTER_DATE=”2019-09-03T14:00:00″ git commit –date “2019-09-03T14:00:00” -m “Initial Commit”
  • git push -u origin main

To remove an existing commit or modify a commit message in a branch:

  • git checkout branch_name
  • git rebase -i master (master is the base_branch_name)
    • It will display a list of all the existing commits with their commit messages
  • Add # infront of any commit message to remove that commit from that branch.
    • You can also change the commit message.
  • Cntrl + X
    • Yes

To abort the rebase:

  • git rebase –abort
  • git rebase –quit

To Rebase to master (basebranch):

  • git checkout branch_name
  • git rebase master
  • git push origin branch -f

To remove local changes and reset to origin branch:

  • git reset –hard “or” git reset –hard origin/branch_name
    • It will reset the branch to the last commit of remote server (Will remove local as well as committed changes)
  • git pull

To Edit the last commit:

  • git reset HEAD~
    • It removes top commit message . Undo the last commit command. Don’t remove the file change.
  • git add files
  • git commit -m “commit msg”
  • git push origin branchname -f

How to set origin with app-token:

  • git remote set-url origin https://avniphadkar:skbuEWQQUw2d5S3rmKU5@bitbucket.org/126interactive/net.eendorsements.git

How to check the list of commits on a set of lines:

  • git blame -L 214,+10 — api/src/Controller/Component/SearchComponent.php

How to check the last commit on a particular line:

  • git log -S’Line goes here’ — api/src/Controller/Component/SearchComponent.php

How to delete a branch:

  • git branch -D branchname (To delete the branch from local)
  • git push origin –delete branchname (To delete the branch from server)

How to copy commits from one branch to another:

  • git checkout anotherbranch
  • git cherry-pick 612ecb3
  • git cherry-pick 453ac3d
  • git cherry-pick bbde871

Remove your uncommitted changes:

  • git reset –hard
  • git reset –hard HEAD (going back to HEAD)
  • git reset –hard HEAD^ (going back to the commit before HEAD)
  • git reset –hard HEAD~2 (going back two commits before HEAD)

How to go back 3 commits OR How to point a branch 3 commits back:

  • git reset –hard HEAD~3 // You *will* lose uncommitted work

  • git log -S’’email_template_assets_path’ => WWW_ROOT . ‘files’,’ — api/config/app.php

How to reset branch-1 to branch-2:

  • git checkout branch-1 // Checkout to branch-1
  • git reset –hard branch-2 // Reset it to branch-2
  • git push –force origin branch-1 // Force push the branch-1

How to rollback git stash:

  • git stash pop

To reset a file to the master branch:

  • git checkout — filename
  • git reset HEAD filename & git checkout — filename // If the file is added to the index but not committed
  • git checkout origin/master filename // If the file is added to the index and committed