0%

Oh, the Git! - Basic

Preface

Git has always been a must-have skill for developers, I will submize a series of blogs related to it, although I don’t know too much about git now.

Pro Git is the best guide of git, I need to read it when I have plenty of time so that I can check for gaps.

VCS(Version Control System)

Central VCS

Central VCS mainly includes SVN and CVS, its newwork architecture is client-server.
Central VCS

Advantages:

  • Centralized version management.
  • Supports file version management and branch management.

Disadvantages:

  • The client must remain connected to the server at all times.

Distributed VCS

Distributed VCS mainly includes Git and Mercurial,its network architecture is distributied.
Distributed VCS

Advantages:

  • Complete repositories on both server and client.
  • The client can manage the version independently.
  • Most operations without relying on the server.

Config

Config user

1
2
3
4
5
6
git config --[ local | global | system ] user.name "your_name"
git config --[ local | global | system ] user.email "your_email@domain.com" # email notification

git config --local # Valid for the current repository, if no option defualts to local
git config --global # Valid for the current user.
git config --system # Valid for all user.

local - .git/config.
global - ~/.gitconfig.
system - git installation path.

Check configuration

1
2
3
git config --list --local # Highest priority
git config --list --global # Higher priority
git config --list --system # Low priority

Clean configuration

1
2
3
git config --unset --local user.name
git config --unset --global user.name
git config --unset --system user.name

Basic usage

Init git repository

1
2
3
4
5
6
cd <project>
git init

or

git init <project>

Work flow

Basic cmds:

1
2
3
4
git add [<file> | .]  # . : All unstracked files of current project.
git commit [--allow-empty] [-m <msg>]
git commit --amend # apend the current staging area to last commit.
git push

Work flow:

Work flow

Tips

I list some commands I often use as follows.

help

1
2
3
4
git help 
git help -a/-g
git <command> --help
git help <command>

Rename

We can rename one files as follows:

1
2
3
mv name new_name
git add new_name
git rm name

The better way:

1
git mv name new_name

log

1
2
3
git log --oneline -n<number> # -n : last <number> record 
git log --onelie --all -n5 --graph # --all: all branches.
git log <branch> # check specified branch

diff

1
2
3
4
5
6
7
8
9
git diff [-- filename] # woking directory compares to staging area.
git diff HEAD # woking directory compares to HEAD
git diff --cached | --staged # staging area compares to HEAD

git diff HEAD HEAD~1 | HEAD^ / HEAD~2 | HEAD^^ | HEAD^2
# '^2' means the second parent
# '~2' means the parent of its parent
# A node can contain multiple sub-nodes (checkout multiple branches)
# A node can have multiple parent nodes (multiple branches merged)

-- of -- filename is to disambiguate.

checkout

1
2
3
4
5
git checkout -b <branch> # Create and checkout <branch>
git checkout # Switch branches or restore working tree files
git checkout . | [--] filename # Restore current git or specified files.
git checkout SHA-1 # Switch to SHA-1, detached HEAD situation.
git checkout -b new_branch base_branch_or_commit # Create and swith new_branch based on branch or commit.

reset

1
2
3
4
5
6
7
git reset HEAD [-- filename]# HEAD and Staging area ponit to HEAD, unstage files.
git reset --hard # clean staging area and working dirctory,

git reset --soft|hard|mixed <commit>
--soft: HEAD points to the specified commit, staging area and woking directory keep as they are.
--hard: HEAD, Staging area and woking directory point to the specified commit.
--mixed: default option, HEAD and Staging area point to the specified commit, woking directory keep as it is.

rebase

Works with detached HEAD.

1
2
3
4
5
git rebase -i start_sha-1  [end_sha-1]
# sha-1 is the oldest commit's parent, whose we want to modify
# If the oldest commit is the first commit, choose the oldest one and add it manully in interactive interface.
# if we specify end_sha-1, one detached HEAD will be created.
# if end_sha-1 is not specified, use the lastest sha-1, all sha-1 from start_sha-1 will change, HEAD and <branch> will point to HEAD of detached HEAD.

An interactive interface will pop up, as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# When we finish the interactive interface, another new interface will pop up.
# The latest commit is at the bottom.

pick 2dc628e modified resume link
pick 0c11aed update resume
pick 375c9df Update in 2020.10
pick 585c3e7 update resume.
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out



stash

1
2
3
4
5
git stash # Stash the changes in a dirty working directory away.
git stash list # List all stash modifications.
git stash pop # Pop the lateset stashed changes.
git stash apply # Apply the lateset stashed changes, and keep it in list.
git stash drop # Delete stash files.

branch

1
2
3
4
5
git branch -v # Check all local branches.
git branch -av # Check all branch included remote branches.
git branch -d | -D brnach
git branch branch_name old_branch | sha1 # Create branch_name based on sha1
git checkout -b new_branch old_branch | sha1

UI

We can check the information of repository through UI as long as we install gitk.

1
2
gitk # open graphical interface.
gitk file

More

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# info from remote repository.
author # who did the first commission, `git cherry-pick` won't change it
committer # who did the last commission.
tag # tag for project stage.

# cmds
git help --web cmd # View cmd's help by web
git tag [-d] <tagname> [<commit>] # Create [delete] a tag reference in refs/tags/.
git commit -am # git add + git commit -m, not suggested.
git reflog # check history version, record when the tips of branches and other references were updated in the local repository.

git show # Shows one or more objects (blobs, trees, tags and commits).
git blame # Show what revision and author last modified each line of a file.

git cat-file -t # Check type of object.
git cat-file -p # Check content of object
find [dir] -type f # Find all files in current/[dir] directory.

- : single char options, like -m , -a;
--: multi char options, like –web, –hard;

.gitignore

github/gitignore.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# c.gitignore
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf