Git

Create

  • git init <project> → create a new directory <project> with git repository
  • git clone ssh://user@domain.com/repo.git → clone an existing repository

Local changes

  • git status → changed files in your working directory
  • git status -s → changed files in your working directory, short view
  • git diff → changes to tracked files
  • git diff --staged → changes to staged files
  • git add . → add all current changes from current directory down
  • git add .. → add all current changes from one directory up to down
  • git add --all → add all current changes in whole repository
  • git add -p <file> → add some changes in <file> to the next
  • git rm <file> → remove <file> also from hard drive
  • git rm --cached <file> → remove <file> only from repository (stays on hard drive)
  • git mv README README.md → git rm README, git add README.md commit
  • git commit -a → commit all local changes in tracked files
  • git commit → commit previously staged changes
  • git commit --amend → change the last commit

Commit history

  • git log → show all commits, starting with newest
  • git log -p <file> → show changes over time for specific <file>
  • git log -2 → show last two commits
  • git log --stat → show summarizing info per commit
  • git log --pretty=oneline / short / full / fuller
  • git log --pretty=format:“%h_%an, %ar : %s”
  • git log --pretty=“%h - %s” → h = hash | s = subject
  • git log --graph → shows commits with graph
  • git log --since=2.weeks → show all commits since two weeks
  • git log --until=3.weeks → show all commits until three weeks
  • git log --author=<user> → show all commits from <user>
  • git log --grep ‘commit name’ → filter commits with the commit name
  • git log origin/master..HEAD → show commits which weren’t pushed yet
  • git log -Sfunction_name → show all commits with this function in it
  • git blame <file> → who changed what and when in <file>

Branches and tags

  • git branch -av → list all existing branches
  • git branch <new-branch> → create a new branch based on your current HEAD
  • git branch -d <branch> → delete a local branch
  • git checkout <branch> → switch HEAD branch
  • git checkout --track <remote/branch> → create a new tracking branch based on a remote branch
  • git tag → lists all tags
  • git tag <tag-name> → make a current commit with a tag
  • git tag -a v1.0 -m ‘my version 1.0’ → create anotated tag
  • git tag v1.4_lw → create lightweight tag
  • git tag -a v0.6 9cf84 → put tag to particullar commit
  • git show v 1.0
  • git push origin <tagname> → push particullar tag
  • git push origin --tags → push all the tags

Update and publish

  • git remote → lists the shortname of each remote
  • git remote -v → list all currently configured remotes
  • git remote add <shortname> <url> → add new remote repository
  • git remote show <remote> → show info about a remote
  • git remote rename pb paul → rename remote from pb to paul
  • git remote rm paul → remove remote paul
  • git fetch <remote> → get all changes from remote (BUT NOT MERGE)
  • git pull → get all changes from remote and MERGE
  • git push <remote> <branch> → publish local changes on a remote
  • git branch -dr <remote/branch> → delete a branch on the remote

Merge and rebase

  • git merge <branch> → merge <branch> into your current HEAD
  • git rebase <branch> → rebase your current HEAD onto <branch>
  • git rebase --abort → abort rebase
  • git rebase --continue → continue a rebase after resolving conflicts
  • git mergetool → use your configured merge tool to solve conflicts

Undo

  • git checkout HEAD <file> → discard local changes in a specific file
  • git revert <commit> → revert a commit (by producing a new commit with contrary changes)
  • git reset --hard HEAD → discard all local changes in you working directory
  • git reset --hard <commit> → reset your HEAD pointer to a previous commit …and discard all changes since then
  • git reset <commit> → …and preserve all changes as unstaged changes
  • git reset --keep <commit> → …and preserve uncommitted local changes

Config

  • git config --global user.name “bendo”
  • git config --global user.email bendo@gmail.com
    global → write into config and ~
    system → write into etc
    without anything → write only to project
  • git config --list → shows settings
  • git config user.name → shows user name from settings