Basic git command

2015/11/07

Display git log

Display git log information nicely git log --oneline --decorate --graph

where oneline show commit in one line, --decorate give some color and --graph gives graph show.

Display git branches

  • Show all locals and remote branchs git branch -a

  • Show all locals branchs only git branch

  • Show all remote branchs only git branch -r

Remove/delete git branch

You can remove either local or remote branch

Removing local branch

Let you want to delete your locale branch named foo git branch -d foo

If branch foo have un merged commit then it need to force delete git branch -D foo

Removing remote branch

You can also use one of the following methods to delete the remote branch. Let you want to delete remote branch bar git push --delete origin bar

Which does the same thing as

git push origin :serverfix but it may be easier to remember.

Create and checkout to new branch:

git checkout -b feature/profile-image-crop

Show last git commit

  • git show --summray this will show commit message
  • git show --stat this also show last git commit with change file list

squashing

squash means combine couple of commits. Let you commit 5 commits to implement a feature but some of the commit was only for typo. If you are thinking that all your code should be in one commit. Using squash you can do it.

Let your feature branch name is ‘foo-feature’ and your base branch name is ‘master’

$ git log --oneline origin/master..foo-feature
73bbc09 add migratio
f33b240 do somthimg
f33b301 solve type mistake

Above command will show your all commits that you had made for implementing foo-feature feature.

git rebase -i origin/master

This will open your editor with these contents:

73bbc09 add migratio
f33b240 do somthimg
f33b301 solve type mistake

# Rebase e54a9a9..73bbc09 onto e54a9a9
#
# 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
# ...

Change the pick to squash (or just s) (those commits that you want to squash, at-least leave top commit), save the file and exit. You’ll then get another editor with a commit message to edit:

# This is a combination of 3 commits.
# The first commit's message is:

Create your commit message and save. That’s it.

branch search

Let you have 40+ branche and most of the time you forget exact branch name. You can search your branc name in different ways

Using grep

git branch | grep foo

above command will return those branches that contain foo word

You can just use git branch --list <pattern> where <pattern> can contain wildcards: it branch --all --list *Theme*

The --all option will include remote branches in the search.

set push.default option

  • previous push.default was matchin that means git push will push all local branches to matching branches( if does not explicitly set brach)
  • new push.default is simple that means git push will push only current local branches to matching branches( if does not explicitly set brach)

Post Directory