1. TL;DR: What happened?
Here’s a set of snippets/notes for intermediate git users, based on the git CLI in linux or git-bash in windows. The remote repo is assumed to be a Github repo.
2. Resources
3. Git concepts
Branches
Working tree vs index?
- Working tree
Directory containing all the files (i.e. the current status of your filesystem).
- Index)
Those that appear green(tracked) when using git status
.
- HEAD
Head is the pointer to the commit from which the working tree’s current state (files) was generated.
If HEAD is not referring to a commit on a branch, it is detached.
Example:
.git/HEAD
contains a ref to .git/refs/heads/master
$ cat .git/HEAD
ref: refs/heads/master
$ cat .git/refs/heads/master
35ede5c916f88d8ba5a9dd6afd69fcaf773f70ed
HEAD refers to a second file named refs/heads/master. The file refs/heads/master contains the hash of the most recent commit on the master branch.
When you check out a branch, HEAD will change.
If HEAD is not referring to a
3. Git commands
Git diff
When you run git diff
with no options, it’s showing you the difference between your current files and the most recent index.
When you run git diff HEAD
it’s showing you the difference between your current files and the most recent commit
Pretty one-liner for git log
git log --pretty=oneline
much more readable.
Add changes matching a RegEx
Use git diff -U0 | grepdiff -E '# .+' --output-matching=hunk | git apply --cached --unidiff-zero
git diff -U0
ommit unchanged lines-E
accepts posix extended regular expressiongit apply
If grepdiff
is not installed, you can get it through sudo apt install patchutils
on debian distros.
4. Github
Github and git should technically be separate,