# Git Book [Git Book](https://git-scm.com/book/en/v2) ## Chapter 1 * Git tracks snapshots, not differences (virtual filesystem) * Nearly every operation is local * Has integrity included * Generally only adds data ### Three states | Working Directory | Staging area | .git repository | |------------------:|:------------:|:----------------| | <= | = checkout= | =< | | stage >= | => | | | | commit >= | => | ### First time setup * Global: /etc/gitconfig * Per-user: .gitconfig * Per-project: .git/config ``` $ git config --global user.name "John Doe" $ git config --global user.email johndoe@example.com $ git config --global core.editor vim $ git config --list ``` ### Get help * git help 'verb' * git 'verb' --help * man git-'verb' ## Chapter 2 ### Getting a Repository * New repositor: `$ git init` * Clone existing: `$ git clone https://github.com/libgit2/libgit2 [mygitlib]` ### Reordering Changes | Untracked | Unmodified | Modified | Staged | |----------:|:----------:|:--------:|:-------| | add the file >= | == | == | => | | | edit the file >= | => | | | | | stage the file >= | => | | <= | =< remove the file | | | | | <= | == | =< commit the file | * Checking the status: `$ git status` * Adding file(s) `$ git add .` `$ git add README.md` `$ git add *.c` * Staging modified files `$ git add modified.file` * The same file can be staged and unstaged, if it was edited after staging! * Short status `git status -s` - Left letter: status of the staging area - Right letter: status of the working tree - ??: untracked - A : added to the staging area - MM: modified, staged, modified again - M: modified, not yet staged - M : modified and staged * ignore files over .gitignore Examples: - `*.a`: no a-files... - `!lib.a`: ...except for lib.a - `/TODO`: ignore TODO in the current directory, but not in other directories - `build/`: ignore all files in the build directory - `doc/*.txt`: ignore txt files in the doc directory - `doc/**/*.pdf`: ignore txt files in doc and all its subdirs ### View staged and unstaged changes * See unstaged changes: `$ git diff` * See staged changed: `$ git diff --staged` or `$ git diff --cached` ### Committing your changes * Commit staged files: `$ git commit` or `$ got commit -m "my message"` * Skip the staging area: `$ git commit -a -m "my message"` - it's equivalent to `git add .` followed by `$ git commit -m "my message"` ### Removing files * `$ git rm` removes files from the _staging_ area and removes it from the drive. * if you first remove a file with `$ rm` the change will be recorded. You must still type `$ git rm` to remove it from the stage area before committing. * if you modified a file and already added it to the staging area, you must use -f to force the removal. This is a security feature, because the data couldn't be recovered by git. * You can remove files from the staging area but keep it on the drive (e.g. if you forgot to put them in .gitignore). `$ git rm --cached` ### Moving files * Git is smart about file moves. therefore the two options are equivalent: - `$ git mv a b` - `$ mv a b` followed by `$ git rm a && git add b` ### Viewing the history * Generally with `$ git log` * show diffs of changes and limit to last N entries: `$ git log -p -N` * show abbreviated stats: `$ git log --stat` * use --pretty=keyword option. Keywords are - oneline - short - full - fuller - format ``` $ git log --pretty=format:"%h - %an, %ar : %s" %H, %h: Commit hash, abbrev. commit hash %T, %t: Tree hash, abbrev. tree hash %P, %p: Parent hash, abbrev. parent hash %an, %ae: Author name, author email %ad, %ar: Author date, author date, relative %cn, %ce: Committer name, committer email %cd, %cr: Committer date, committer date, relative %s: Subject ``` * Show graph: `git log --pretty=format:"%h %s" --graph` * other options: - -(n) show only the last n commits - --since, --after Limit to commits after a date - --until, --before Limit to commits before a date - --author Filter by author - --committer Filter by committer - --grep Filter by content of the commit string - -S Only show commits adding or removing code matching the string