Notes on Git

Install

Git website


Configure

git-config – Get and set repository or global options

  • System level: git config --system
  • User level: git config --global
  • Project level: git config

Important settings

  • Set: git config --global user.name "<Name>"
  • Set: git config --global user.email "<Email>"
  • Set editor: git config --global core.editor "<editor>"
  • Use colors in bash: git config --global color.ui true
  • Create / Edit .bash_profile and insert e.g. **export PS1='\[\e[1;34m\]\t \[\e[1;31m\]\u \[\e[32m\]\w\[\e[33m\]`__git_ps1` \[\e[0m\]\n$ ', where:
    • \d – Current date
    • \t – Current time
    • \h – Host name
    • \# – Command number
    • \u – User name
    • \W – Current working directory (ie: Desktop/)
    • \w – Current working directory with full path (ie: /Users/Admin/Desktop/)
    • '\[\e[1;31m\]' – RED
    • '\[\e[1;33m\]' – BOLDYELLOW
    • '\[\e[0;32m\]' – GREEN
    • '\[\e[1;34m\]' – BLUE
    • '\[\e[1;33m\]' – DARKBROWN
    • '\[\e[1;30m\]' – DARKGRAY
    • '\[\e[1;30m\]' – CUSTOMCOLORMIX
    • '\[\e[1;32m\]' – DARKCUSTOMCOLORMIX
    • '\[\033[1;36m\]' – LIGHTBLUE
    • '\[\e[1;35m\]' – PURPLE
    • '\[\e[0;32m\]' – GREEN
    • '\[\e[1;35m\]' – PURPLE
    • '\[\e[1;34m\]' – BLUE
    • '\[\e[0m\]' – NO COLOR

Display settings

To show a specific setting, use the set command without the value, e.g.:

  • git config --global user.name
  • git config --global color.ui

Help

git help – Display help information about Git

  • git help <command>
  • man git-<command>

Initialize

git init – Create an empty Git repository or reinitialize an existing one

  • git init – Creates a .git directory where Git data are stored.

Status

git status – Show the working tree status

  • git status – Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git (and are not ignored by gitignore).

Ignore

  • .gitignore – Define content to be ignored using specific names and / or simple regular expressions.
    • * ? [a-z] [0-9]“**
    • ! negates
    • <dir>/ – Ignore all files in \<dir>
    • # – comments lines, blank lines are ignored
  • Does not work on already commited content. To ignore such content use git rm --cached <content>
  • By default, empty directories are not tracked. In order for them to be tracked an empty .gitkeep file can be created in them just to make them not empty – touch .gitkeep.

Add

git add – Add file contents to the staging index

  • git add . – Stages all changes for the next commit.
  • git add <file> – Stages \<file> changes for the next commit.
  • git add <file1> <file2> <...> – Stages \<file1>, \<file2>, … changes for the next commit.

Unstage

git reset HEAD – Unstage \<file>.

  • git reset HEAD <file> – Unstages \<file>.

Remove

git rm – Remove files from the working tree and from the index

  • git rm <file> – Removes \<file> from the index, or from the working tree and the index. Not inversible if commited!!!

Move / Rename

git mv – Move or rename a file, a directory, or a symlink

  • git mv <source> <destination> – Moves \<source> to\<destination>.

Clean

git clean – Remove untracked files from the working tree

  • git clean -n – Don’t actually remove anything, just show what would be done.
  • git clean -f – Forcefully removes files that are not tracked.

Commit

git commit – Record changes to the repository

  • git commit -m "<message>" – Commit staged changes with \<message>.
  • \<message> should:
    • have a short first line as title which is followed by a summary.
    • be in present tense
  • HEAD points to the "tip" of the current branch.
    • is located at .git/HEAD -> refs/heads/master contains SHA1

Reference commits

  • treeish – a reference to a commit:
    • a full SHA-1 hash
    • a short SHA-1 hash:
    • at least 4 chars
    • unambiguous (8-10 chars)
    • HEAD
    • branch reference
    • tag reference
    • ancestry:
    • ^ – eg HEAD^ refers to the parent of HEAD, HEAD^^ refers to its grandparent
    • ~ – eg HEAD~1 for parent, HEAD~2 for grandparent

Amend commit

git commit –amend – Replace the tip of the current branch by creating a new commit

  • git commit --amend -m "<message>" – Using the same \<message> as the commit to be amended, amends the commits by commiting the staged changes.
    Can also be used to just amend the message.
    Works only for the HEAD!

Revert commit

git revert – Revert some existing commits

  • git revert <SHA> – Reverses the changes of the commit. Works on a clean working directory only!
  • git revert <SHA> -n – Reverses the changes of the commit to the working directory but does not commit them!

Reset

git reset – Reset current HEAD to the specified state

Soft Reset

  • git reset --soft <SHA> :
    • moves HEAD pointer,
    • does not change staging index,
    • does not change working directory.

Mixed Reset

  • git reset --mixed <SHA> :
    • the default reset,
    • moves HEAD pointer,
    • changes staging index,
    • does not change working directory.

Hard Reset

  • git reset --hard <SHA> :
    • moves HEAD pointer,
    • changes staging index,
    • changes working directory.

Differences

git diff – Show changes between commits, commit and working tree, etc

  • git diff – Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes between two blob objects, or changes between two files on disk.
  • git diff --staged – Compares staging index with the repository.
  • git diff <SHA> – Shows differences between \<SHA> and HEAD.
  • git diff <SHA> <file> – Shows differences of \<file> between \<SHA> and HEAD.
  • git diff <SHA1>..<SHA2> – Shows differences between \<SHA1> and \<SHA2>.
  • git diff --color-words <SHA1>..<SHA2> – Shows differences between \<SHA1> and \<SHA2> in one line for each, using different colors.

Checkout

git checkout> – Switch branches or restore working tree files

  • git checkout -- <file> – Checkouts \<file> from HEAD.
    The -- ensures that \<file> not a branch with the same name is checked out. Puts \<file> directly into staging area.
  • git checkout <SHA> -- <file> – Checkouts \<file> from \<SHA> commit.

Remote

  • git checkout -b <local> <remote> – Checks out and branches \<remote> locally as \<local>.

Branch

git branch – List, create, or delete branches

  • git branch – Shows all branches and marks active branch with a *.
  • git branch <branchname> – Creates branch \<branchname>.
  • git checkout <branchname> – Switches to branch \<branchname>.
    Switching is only allowed when the working directory is mostly clean, i.e. there are no changes in tracked files.
  • git checkout -b <branchname> – Creates branch \<branchname> and switches to it.
  • git branch --merged displays all branches included in current branch.
  • git branch -m <branchname1> <branchname2> – Renames \<branchname1> to \<branchname2>.

Delete a branch

  • git branch -d <branchname> – Deletes \<branchname> provided:
    • It is not the current branch.
    • It is fully merged. -D overides this.

Remote branches

  • git branch -r – Shows remote branches.
  • git branch -a – Shows local and remote branches.

Merge

git merge – Join two or more development histories together

  • git merge <branch> – Merges \<branch> to the current branch.
  • git merge --no-ff <branch> – Merges \<branch> to the current branch not as a fast-forward even if it could be made as fast-forward.
  • git merge --ff-only <branch> – Merges \<branch> to the current branch only if it can be made as a fast-forward commit.
  • git merge --abort – Aborts a merge while merge conflicts exist.
  • After you have dealt with the merge conflicts in the corresponding files, you git add these files and then you git commit without a message, in order to complete the merge.
  • git mergetool List candidate tools for solving merging conflicts. It’s good to first get experience in solving conflicts without them.
  • Merge often and keep your branch up to date with the master branch (tracking).

Remote merge

  • git merge <alias> – Merges locally commited changes from the remote repository \<alias>, after a git fetch <alias>.

Stash

git stash – Stash the changes in a dirty working directory away

  • git stash save "<message>" – Stashes changes with \<message>.
  • git stash list – Lists items in stash.
  • git stash show <stash> – Shows summary of \<stash> changes.
  • git stash show -p <stash> – Shows \<stash> changes, like diff.
  • git stash pop <stash> – Removes \<stash> changes from stash and applies them to current branch. Without \<stash> it pops the first stash.
  • git stash apply <stash> – Applies \<stash> stash changes, without removing them. Without \<stash> it applies the first stash.
  • git stash drop <stash> – Removes \<stash> changes from stash.
  • git stash clear – Clears, ie removes everything from the stash.

Log

git log – Show commit logs

  • git log – Shows sha1, Author and Date.
  • git log -n <no> – Shows \<no> previous commits.
  • git log --since=<date> – Shows commits since \<date>.
  • git log --after=<date> – Shows commits after \<date>.
  • git log --until=<date> – Shows commits until \<date>.
  • git log --before=<date> – Shows commits before \<date>.
  • Types of \<date>:
    • <yyyy>-<mm>-<dd>
    • **<no> days ago
    • **<no> weeks ago
  • git log --author="<name>" – Shows commits by \<name>.
  • git log --grep="<text>" – Shows commits with \<text> in their commit message.
  • git log --oneline – Shows one line per commit.
  • git log --oneline -<n> – Shows one line per commit, for the last \<n> commits.
  • git log --oneline <SHA 1>..<SHA 2> – Shows the commits between \<SHA 1> and \<SHA 2>.
  • git log --oneline <SHA 1>.. <file> – Shows the changes after \<SHA 1> for \<file>.
  • git log --stat --summary – Shows statistics and summaries of commits.
  • git log --graph – Shows a graph of commits.
  • git log --oneline --graph --all --decorate – Nice compact list!

List Tree

git ls-tree – List the contents of a tree object

  • git ls-tree <tree-ish> – Shows the contents of the tree object refered to by the \<tree-ish>.
  • git ls-tree <tree-ish> <dir>/ – Shows the contents of the \<dir> of the tree object refered to by the \<tree-ish>.

Show

git show – Show various types of objects

  • git show <SHA> – Shows what was commited in \<SHA>.

Remotes

GitHub

  • Create account in http://github.com
  • Create a new repository.

Remote

git remote – Manage set of tracked repositories.

  • git remote – Displays remotes.
  • git remote add <alias> <url/ssh> – Adds remote at \<url/ssh> with alias \<alias>.

Github SSH

  • git remote add <alias> git@github.com:<user-name>/<repository name>.git

    You can add more than one remotes.

    • git remote rm <alias> – Removes the link to the remote \<alias>.

Push

git push – Update remote refs along with associated objects.

  • git push -u <alias> <branch> – Pushes \<branch> to \<alias> creating a new branch at \<alias> and, because of the -u starts tracking it.
  • After executing the above, you can just use git push to push changes.
  • A push cannot be done if the remote has changed, you need to fetch and merge first.

Fetch

git fetch – Download objects and refs from another repository.

  • git fetch <alias> – Gets and commits locally changes from the \<alias> remote repository, without merging them.

Merge

Merge

  • git merge <alias> – Merges locally commited changes from the remote repository \<alias>, after a git fetch <alias>.

Pull

  • git pull <alias> – Gets, commits and merges locally changes from the \<alias> remote repository.
    • It combines a git fetch <alias> and a git merge <alias>.

Branch

git branch – List, create, or delete branches

  • git branch -r – Shows remote branches.
  • git branch -a – Shows local and remote branches.
  • git branch <local> <remote> – Branches \<remote> locally as \<local>.

Checkout

git checkout> – Switch branches or restore working tree files

  • git checkout -b <local> <remote> – Checks out and branches \<remote> locally as \<local>.

Clone

git clone – Clone a repository into a new directory.

  • git clone <url> <dir> – Clone the repository at \<url> at \<dir>. If \<dir> is not specified it creates a dir at current position.

Remove

  • git push <remote> :<branch> – Removes \<branch> at \<remote>, as it pushes nothing to the remote \<branch> because there is nothing before the :.
  • git push <remote> --delete <branch> – Removes \<branch> at \<remote>.

Collaborators

  • To add collaborators to a GitHub project, go to Settings \ Collaborators.

Tools

Aliases

git-config – Get and set repository or global options

  • git config --global alias.<alias> "<command>" – Creates a global \<alias> for \<command>. Useful examples:
    • git config --global alias.co checkout
    • git config --global alias.ci commit
    • git config --global alias.br branch
    • git config --global alias.df diff
    • git config --global alias.dfs diff --staged
    • git config --global alias.dfc diff --cached
    • git config --global alias.logg "log --graph --decorate --oneline --abbrev-commit --all"

SSH