Cloning a repository;

Checkout

# Going back in history. Find change and checkout
git hist
git checkout <hash>

# To return to the latest version in master
git checkout master

Creating tags

git tag <tag name>

#to remove it
git tag -d <tag name>

Creating a branch

git checkout -b <branchname>

Command above is a shortcut for

git branch <branchname>
git checkout <branchname>

To switch between branches

git checkout master

To merge back a branch

git checkout <branch-name>
git merge master

Resetting changes

#after the change has been staged
git reset HEAD hello.rb
git checkout hello.rb

# after it has committed
git revert HEAD

Creating Alias:

Add the following to the .gitconfig file in your $HOME directory.

[alias]
  co = checkout
  ci = commit
  st = status
  br = branch
  hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
  type = cat-file -t

Or using cli:

git config --global alias.log --oneline --graph --decorate --all
git config --global alias.hist 'log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short'

Git Logs Formatting

git log --pretty=oneline --max-count=2
git log --pretty=oneline --since='5 minutes ago'
git log --pretty=oneline --until='5 minutes ago'
git log --pretty=oneline --author=<your name>
git log --pretty=oneline --all

# changes made in the last week.
git log --all --pretty=format:'%h %cd %s (%an)' --since='7 days ago'

git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

Let’s look at it in detail:

  • --pretty="..." defines the format of the output.
  • %h is the abbreviated hash of the commit
  • %d are any decorations on that commit (e.g. branch heads or tags)
  • %ad is the author date
  • %s is the comment
  • %an is the author name
  • --graph informs git to display the commit tree in an ASCII graph layout
  • --date=short keeps the date format nice and short