Plan A B C  • • •  Review of 考试脑科学       all posts in Archive

Git Notes

1. Understanding INDEX

Git introduces INDEX which is quite different with other Version Control tools.

Git WorkingDirectory, Index and ObjectDatabase

  • Git makes copy for the new file in INDEX and OBJECT DATABASE (OD)
  • When all the three (WD,Index,OD) look identical, then the file is marked as unmodified
  • When WD and Index are not the same, means it changed but not staged
  • When WD and Index look the same, means Staged
  • When Index and OD dosen’t match, means still modified

2. Understanding HEAD

2.1 What is HEAD

Git HEAD

  • HEAD is simply a reference, like branches are. The difference is Git repository can have many branches, but there is only one HEAD.
  • HEAD also serves as the lauch point to decide how the commit history will change, in that the commit that HEAD points to will be the parent of the next commit - it’s how Git knows where to add the new commit in the commit history.
  • when on master branch, HEAD is point to C
  • when swith to feat branch, HEAD will point to D

A number N following the tilde operation represents the Nth geneartional ancestor. For example:

HEAD~1, HEAD~2, HEAD~3 ...

We can check HEAD Reference Log with:

git reflog

2.2 What is Detached HEAD

It will happen when we checkout a old commit like: git checkout 8012821beca64fd6efbd404a11317a0705d6000d, Git will show notification as:

Note: switching to '8012821beca64fd6efbd404a11317a0705d6000d'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

It simply means we are no longer on a brach, and remeber HEAD points to will be the parent of the next commit.

The best practise is be sure to craete a branch first and switch to it before making any edits hwne in detached HEAD state.

3. Undoing changes

How to undoing Changes in Git

4. Git Remote

4.1 Check where to push:

$ git remote -vv
origin	git@github.com:villim/villim.github.io.git (fetch)
origin	git@github.com:villim/villim.github.io.git (push)

4.2 Change remote branch conf:

$ git push --set-upstream origin feat-a

4.3 Cleaning up remote branches:

$ git remote prune origin
$ git fetch -p

4.4 Fetch & Pull

$ git fetch  # updating the remote traching branches
$ git pull  # equals git fetch + git merge

5. Git a Grep

5.1 Seeing who changed what and when

$ git blame file-name
$ git blame HEAD file-name

git blame can tell you when a particular line was last changed, but can’t tell you what changed in that line when that commit was made. And it only looks at one file at a time.

If we need check a particular piece of text was added or moreved, we need git log command.

5.2 Git log’s “pickaxe” capability

$ git log -S key-word
$ git log -S key-word file-name  # search in a file
$ git log -p -S key-word  ## with --patch flag to display actural diff introduced
$ git log --oneline -p -S key-word  --word-diff ## word-diff flag shows how individula words differ rather than whole lines
$ git log -G key-word  # searches for key-word in any commit's diff - if line that the text apprears on was changed in any way

5.3 Searching Commit Messages

$ git log --oneline --all --graph -p  # display all log in graph with patches
$ git log --grep key-word
$ git log --grep "key word"  # double quotes for searching for a phrase

5.2 Searching Git repositories

$ git grep key-word
$ git grep "key-word"  # the quotes tell Git where the phrase starts and end
$ git grep -i key-word  # look for key-word ignoring case
$ git grep -l key-word  # list only filenames, or using --name-only
$ git grep -n key-word  # list the line numbers

6. About Merging: Rebase & Merge

Git Merge is easy to understand.

Git Merge

$ git merge feat-branch
$ git merge --squash feat-branch  # Squashing is to clean up and simplify your commit history

Git Rebase: as the name implies, this command allows you to merge two branches by moving one branch on top of another.

Git Rebase

$ git rebase master
$ git rebase -i master  # -i means to enter into interactive mode of rebase

Infact, there’s another way to merge commit: Using cherry-pick for copying commits

7. Git Tips

7.1 Git Configuration

How to configure Git

Note we can setup gloabal settings and project settings at the same time.

7.2 Branch Name Best Practice

Creating helpful branch name like: prefix/ticket number/shortdescription of the story with hyphen, for example:

vw/1618/remove-typos-in-documentation

7.3 Commit Message Best Practice

Commit Message contains: commit type + commit message + ticket number

  • Commit type: feat, chore, docs, fix
  • Commit message: short description for the change
  • Ticket number: could be the Jira ID

For Example:

feat: update css style name to be consistent (#6174)

( one blank line to separating header with body )											
- Aligns the names for all css.         ( each list is prefixed with hyphen )
- Ensures camel - case styling        ( each list is prefixed with hyphen )

7.5 Save changes without commit to a branch

$ git stash
$ git stash pop

7.6 Clone to an existing folder

clone a repo in an existing folder that already contains files/folders, try this:

cd <project-folder>
git init
git remote add origin <your-repo>
git pull
git checkout master -f

8. Git GUI

  • Sourcetree
  • Tig Tig is an ncurses-based text-mode interface for git.