Git 教學 (Git Tutorial)

Git 教學 (Git Tutorial)

git 教學與筆記。

Basic settings

1
2
3
4
5
6
7
8
9
10
11
git --version

git config --global user.name MeowLucian
git config --global user.email Meow.Lucian@gmail.com
git config --list
git config --global --edit
git config --global core.editor "vim"
git config --global --unset core.editor
git config --global --add safe.directory '*'

http."https://chipmaster2.qti.qualcomm.com".followRedirects true

Init

1
git init

ssh-key

1
2
3
4
cd ~/.ssh
ssh-keygen
cat id_rsa.pub
Gitlab -> Profile Settings -> SSH Keys -> 貼上

Clone

1
2
3
4
5
git clone -b "<specific branch>" --single-branch "<git repository url>" "<local folder name>"

# Undo --single-branch clone
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin

Add

1
2
git add "<file>"
git add -f .

Status

1
git status .

Commit

1
2
git commit -m "<commit name>"
git commit --amend

Patch

1
2
3
git format-patch -1 <commit> -o ~/test_code/patch      # 從包含 <commit> 往前算共 1 個 commit
git format-patch -1 <commit> -o ~/ -- "<folder>" # 只包含某 <folder> 的變化
git am test.patch

Cherry-pick

1
git cherry-pick "<commit ID>".."<commit ID>"     # (注意第一個編號要是前一個commit)

Log

1
2
3
4
5
6
7
8
9
git log
git log "<file>"
git log -p "<file>"
git log --all -p "<file>" # Find from all branchs
git log --oneline -2
git log --pretty="PIC:%an, %s"
git log --grep "test"
git log --stat
git log --author=Lucian

Show

1
2
3
git show "<commit>" --stat
git show --pretty="" --name-only "<commit>"
git show "<commit>":"<file>"

Diff

1
2
3
4
git diff "<commit>" "<commit>"
git diff --stat
git diff --staged "<file>"
git diff --name-only .

grep

1
2
git ls-files | fgrep "test.c"
git grep -n -i --files-with-matches "<text>"

Checkout

1
git checkout "<file>"

Reset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
git reset HEAD --hard

# 還原到前一個commit
git reset HEAD^

# 還原到前二個commit
git reset HEAD^^

# 還原到前二個commit
git reset HEAD~2

git reset HEAD "<file>"

git reset "<commit>" "<file>" # reset a specific file to a specific revision

Clean

1
git clean -f -d

Branch

1
2
3
4
5
6
7
8
9
10
git branch -a
git checkout "<new branch name>"
git checkout -b "<new branch name>" "<started commit ID>"
git branch --contains "<commit>" # find branch with specific commit

# Find <commit> in all branches
git branch -a --contains <commit>

# Delete branch
git branch -d "<branch name>"

Merge

1
git merge "<branch name>"

Remote

1
2
3
4
5
6
git remote -v
git remote add "<local nickname>" "<git repository url>"
# Local
git remote add TEST /build/lucian/Project/test

git remote remove origin

Push

1
2
git push "<remote>" "<commit SHA>":"<remote branch>"
git push origin 19cbe78:master

Fetch

1
git fetch origin

Pull

1
git pull

Rebase

1
2
3
4
5
6
7
8
9
10
11
12
git rebase -i "<commit>"

pick "<commit_1>"
reword "<commit_2>" # Change commit name
pick "<commit_3>"
s "<commit_4>" # Squash

edit "<commit_5>" # Rebase interactive mode

# Change Commit Author Name example
git commit --amend --author="MeowLucian <Meow.lucian@gmail.com>" --no-edit
git rebase --continue

Tag

1
2
3
4
5
6
git tag
git tag -l
git tag -n
git tag "<tag ID>" # Add new tag
git tag -d "<tag ID>" # Delete tag
git tag -am "<tag comment>" "<tag ID>"

Stash 封存

1
2
3
4
5
6
7
8
9
git add .
git stash save 'stash 1'

git stash list

git stash pop stash@{0}

git stash drop
git stash clear

Gerrit

1
2
3
4
git review -R

cat ~/.ssh/config (必須存在)
User 19002347

Share the code

https://gist.github.com/

Upgrade to latest git version

1
2
3
4
5
6
(option) sudo rm -rf /usr/share/ca-certificates
(option) sudo apt-get --reinstall install ca-certificates

sudo add-apt-repository ppa:git-core/ppa -y
sudo apt-get update
sudo apt-get install git -y

Fix denyCurrentBranch issue

  • Error message :

    1
    2
    remote: error: refusing to update checked out branch: refs/heads/master
    remote: error: By default, updating the current branch in a non-bare repository
  • Solution : Add below setting in ~/.gitconfig

    1
    2
    [receive]
    denyCurrentBranch = ignore
Author

Meow Lucian

Posted on

2019-04-24

Updated on

2022-08-07

Licensed under

Comments