Git in Day to Day Life

Here is the quick list of commands that one need to use in day to day life. As I keep forgetting them, thought of listing down in one place.  Hopefully this list may save my time ;)

Configuring your identity 

$ git config --global user.name "FirstName LastName"
$ git config --global user.email firstname.lastname@somedomain.com

Clone repository

$ git clone

Create new branch

$ git  checkout -b feature/branch-name

Staging and committing changes

$ git  add .
$ git  commit -m "comments"
$ git push -u origin feature/branch-name

Viewing git log

$ git log --after="2020-01-01 00:00"
$ git log --all --pretty=format:"%h %cd %s (%an)" --since=7.days
$ git log --all --pretty=format:"%h %cd %s (%an)" --since='7 days ago'

Rebasing a feature branch

$ git checkout master
$ git pull
$ git checkout feature/branch-name
$ git rebase master
$ git add
$ git rebase --continue
$ git rebase master
$ git push origin feature/branch-name --force

Git pull with rebase

$ git pull --rebase

To abort rebase

$ git rebase --abort

Merging feature branch with master

$ git merge master

Squashing commits

$ git config --global rebase.instructionFormat "(%an <%ae>) %s"
$ git rebase -i HEAD~n

pick xxxxxxx Some commit comment1
pick xxxxxxx Some commit comment2

# Rebase xxxxxxx.. xxxxxxx onto xxxxxxx
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

$ git push origin feature/branch-name --force

Squashing All Commits on a Branch

$ git checkout feature/branch-name
$ git reset --soft id-of-first-revision-you-want-to-retain
$ git commit --amend -m "single commit for the branch"
$ git push origin feature/branch-name --force

Cherry pick a commit from master branch

$ git checkout feature/branch-name
$ git cherry-pick -x <commit-hash>

Reverting a commit

$ git revert <commit-hash>

Reverting changes made to a file in a Pull Request

$git checkout origin/master -- <path/fileName>
$git commit -a --amend
$git push -u origin HEAD -f


To checkout a specific commit for testing

$ git checkout <commit-hash>

Replacing master branch with feature brach

$ git checkout feature/branch-name
$ git merge -s ours master
$ git checkout master
$ git merge feature/branch-name

Ignoring file(s) from staging/commit

Add them in to  .git/info/exclude

Comments

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of my employer.