Practical git Tips


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

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,


Author: Zhao Du
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Zhao Du !
 Previous
Deep Learning with Pytorch by NYU (part 1) Deep Learning with Pytorch by NYU (part 1)
Collection of notes about basics of gradient descent, implementation from scratch
2021-08-02
Next 
OpenSUSE Leap 15.3・Windows10のデュアルブートワークステーションをセットアップしてみた OpenSUSE Leap 15.3・Windows10のデュアルブートワークステーションをセットアップしてみた
個人用のためwindows10・OpenSUSEのデュアルブートをセットアップした。勉強してきたもの・落とし穴を整理した。お役に立てるとうれしいです。
2021-07-13
  TOC