Git學習筆記

利用Git進行協同開發:
例如項目主線master起始就有兩個文件:1.txt / 2.txt 。分三個team進行開發不同功能。
branch1首先開發好了4.txt。branch3比較懶,啥也沒幹。branch2第二個開發好了最後一個功能3.txt。於是branch2需要在branch1的基礎上進行rebase,merge。最後release。版本穩定後,再merge到master上。
master一般只接受最穩定的版本~

從Concepts學習git:https://jwiegley.github.io/git-from-the-bottom-up/

Git / GitLab / Github
三人協作開發:不一定需要用Github的服務器作爲平臺。可以在自己的服務器上部署一個GitLab。

cd ~
mkdir .ssh
cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]"
cat id_rsa.pub
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
ssh -T [email protected]

vim config
 Host github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/id_rsa

cd ~
cd Desktop
mkdir gittest
cd gittest
git clone [email protected]:DrKLO/Telegram.git


git log
gitk
git branch -r:查看遠端分支
git branch -a:查看所有分支
git config --list
$ git remote -v
origin  [email protected]:DrKLO/Telegram.git (fetch)
origin  [email protected]:DrKLO/Telegram.git (push)
(view)

git add 相當於提交到cache
git rm = rm 文件 + git add 文件
git pull 破壞性(遠端拉下來,等於fetch+merge本地) / git fetch 指定分支 / git clone 全量
不同team要rebase的時候一般使用git fetch。
85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git fetch origin dev
From github.com:DrKLO/Telegram
 * branch              dev        -> FETCH_HEAD
git rebase FETCH_HEAD

git commit --amend:對上一次commit作修正
git commit --amend --author="JACK <[email protected]>"
git commit --amend --reset-author:重置commit文件中的author

查看兩個分支的不同:
85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ git diff ^test123
diff --git a/2.txt b/2.txt
new file mode 100644
index 00000000..e69de29b

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ git checkout test123
Switched to branch 'test123'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git merge test456 --no-ff
Merge made by the 'recursive' strategy.
 2.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2.txt

學習源代碼:

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest
$ git clone [email protected]:DrKLO/Telegram.git
Cloning into 'Telegram'...
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 31984 (delta 0), reused 0 (delta 0), pack-reused 31982
Receiving objects: 100% (31984/31984), 174.27 MiB | 2.16 MiB/s, done.
Resolving deltas: 100% (16309/16309), done.
Updating files: 100% (5913/5913), done.

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (master)
$ git branch -r
  origin/HEAD -> origin/master
  origin/dev
  origin/master

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (master)
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (master)
$ git remote -v
origin  [email protected]:DrKLO/Telegram.git (fetch)
origin  [email protected]:DrKLO/Telegram.git (push)

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (master)
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslcainfo=C:/Software/Git/mingw64/ssl/certs/ca-bundle.crt
http.sslbackend=openssl
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge --skip -- %f
filter.lfs.process=git-lfs filter-process --skip
filter.lfs.required=true
credential.helper=manager
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=[email protected]:DrKLO/Telegram.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (master)
$ git checkout -b localtest
Switched to a new branch 'localtest'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localtest)
$ git branch -a
* localtest
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localtest)
$ gitk

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localtest)
$ git checkout -b localdev origin/dev
Updating files: 100% (6725/6725), done.
Switched to a new branch 'localdev'
Branch 'localdev' set up to track remote branch 'dev' from 'origin'.

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git branch -a
* localdev
  localtest
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ gitk

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git branch
* localdev
  localtest
  master

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git branch -D localtest
Deleted branch localtest (was 28eb8dfd).

~/Desktop/gittest/Telegram/TMessagesProj/src/main/java/org/telegram/SQLite (localdev)
$ vim SQLite
SQLiteCursor.java             SQLiteNoRowException.java
SQLiteDatabase.java           SQLitePreparedStatement.java
SQLiteException.java

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram/TMessagesProj/src/main/java/org/telegram/SQLite (localdev)
$ vim SQLite
SQLiteCursor.java             SQLiteNoRowException.java
SQLiteDatabase.java           SQLitePreparedStatement.java
SQLiteException.java

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram/TMessagesProj/src/main/java/org/telegram/SQLite (localdev)
$ vim SQLiteCursor.java

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram/TMessagesProj/src/main/java/org/telegram/SQLite (localdev)
$ git status
On branch localdev
Your branch is up to date with 'origin/dev'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   SQLiteCursor.java

no changes added to commit (use "git add" and/or "git commit -a")

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram/TMessagesProj/src/main/java/org/telegram/SQLite (localdev)
$ gitk SQLiteCursor.java

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram/TMessagesProj/src/main/java/org/telegram/SQLite (localdev)
$ git add SQLiteCursor.java

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram/TMessagesProj/src/main/java/org/telegram/SQLite (localdev)
$ git status
On branch localdev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   SQLiteCursor.java

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest
$ cd Telegram/

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ touch 1.txt

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git status
On branch localdev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   TMessagesProj/src/main/java/org/telegram/SQLite/SQLiteCursor.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        1.txt


85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git add 1.txt

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git status
On branch localdev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   1.txt
        modified:   TMessagesProj/src/main/java/org/telegram/SQLite/SQLiteCursor.java


85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ ls
1.txt         gradle/            gradlew*  README.md        TMessagesProj/
build.gradle  gradle.properties  LICENSE   settings.gradle  Tools/

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ rm -f README.md

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git status
On branch localdev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   1.txt
        modified:   TMessagesProj/src/main/java/org/telegram/SQLite/SQLiteCursor.java

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    README.md


85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git add README.md

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git status
On branch localdev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   1.txt
        deleted:    README.md
        modified:   TMessagesProj/src/main/java/org/telegram/SQLite/SQLiteCursor.java
        
85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ ls
1.txt         gradle/            gradlew*  settings.gradle  Tools/
build.gradle  gradle.properties  LICENSE   TMessagesProj/

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git rm gradle.properties
rm 'gradle.properties'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git status
On branch localdev
Your branch is up to date with 'origin/dev'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   1.txt
        deleted:    README.md
        modified:   TMessagesProj/src/main/java/org/telegram/SQLite/SQLiteCursor.java
        deleted:    gradle.properties

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git reset --mixed gradle.properties
fatal: ambiguous argument 'gradle.properties': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git commit

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's' default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got '85380@DESKTOP-3S1KOHB.(none)')

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$  git config --global user.email "[email protected]"

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git config --global user.name "Jacobtsang"

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git commit
[localdev a7167452] Fix a bug
 4 files changed, 1 insertion(+), 36 deletions(-)
 create mode 100644 1.txt
 delete mode 100644 README.md
 delete mode 100644 gradle.properties

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git log
commit a7167452d9178453d2e72b584e3ec69b9a9980ca (HEAD -> localdev)
Author: Jacobtsang <[email protected]>
Date:   Sat Oct 26 11:12:56 2019 +0800

    Fix a bug

    description: this is a fix

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git branch
* localdev
  master

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ gitk

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git branch
* localdev
  master
  
85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git fetch origin dev
From github.com:DrKLO/Telegram
 * branch              dev        -> FETCH_HEAD

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ ls
1.txt         gradle/   LICENSE          TMessagesProj/
build.gradle  gradlew*  settings.gradle  Tools/

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ vim settings.gradle

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git commit -a
[localdev d290eb14] Add a comment
 1 file changed, 1 insertion(+)

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git log
commit d290eb144c4769b27e8403df22cf2e3c96748a48 (HEAD -> localdev)
Author: Jacobtsang <[email protected]>
Date:   Sat Oct 26 11:32:18 2019 +0800

    Add a comment

    Description: Comment

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ ls
1.txt         gradle/   LICENSE          TMessagesProj/
build.gradle  gradlew*  settings.gradle  Tools/

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ vim LICENSE

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git status
On branch localdev
Your branch is ahead of 'origin/dev' by 2 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   LICENSE

no changes added to commit (use "git add" and/or "git commit -a")

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git add LICENSE

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git status
On branch localdev
Your branch is ahead of 'origin/dev' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   LICENSE


85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git commit
Aborting commit due to empty commit message.

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git commit --amend
[localdev c9bb0400] Add a Lisence comment
 Date: Sat Oct 26 11:32:18 2019 +0800
 2 files changed, 3 insertions(+), 1 deletion(-)

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git commit --amend --author="JACK <[email protected]>"
[localdev 785b672d] Add a Lisence comment
 Author: JACK <[email protected]>
 Date: Sat Oct 26 11:32:18 2019 +0800
 2 files changed, 3 insertions(+), 1 deletion(-)

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git log
commit 785b672dab20662720615e9458c4387b3c7e63d6 (HEAD -> localdev)
Author: JACK <[email protected]>
Date:   Sat Oct 26 11:32:18 2019 +0800

    Add a Lisence comment

    Description: Comment

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git commit --amend --reset-author
[localdev 4310fe22] Add a Lisence comment
 2 files changed, 3 insertions(+), 1 deletion(-)

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ ls
1.txt         gradle/   LICENSE          TMessagesProj/
build.gradle  gradlew*  settings.gradle  Tools/

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git log
commit 4310fe22d246146f0732d2c0290e6dd1800e35ed (HEAD -> localdev)
Author: Jacobtsang <[email protected]>
Date:   Sat Oct 26 11:37:41 2019 +0800

    Add a Lisence comment

    Description: Comment

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ gitk

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (localdev)
$ git checkout -b test123
Switched to a new branch 'test123'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ gitk

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git branch
  localdev
  master
* test123

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git checkout -b test456 localdev
Switched to a new branch 'test456'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ gitk

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ touch 2.txt

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ git add 2.txt

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ git commit
[test456 1e56b701] add a 2.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2.txt

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ gitk

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ git checkout test123
Switched to branch 'test123'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git merge test456
Updating 4310fe22..1e56b701
Fast-forward
 2.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2.txt

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ gitk


85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git log
commit 1e56b7013e5f315108e8361bef5b7c6b425615bf (HEAD -> test123, test456)
Author: Jacobtsang <[email protected]>
Date:   Sat Oct 26 11:42:57 2019 +0800

    add a 2.txt

    decs: jjj

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git reset --hard 4310fe22d246146f0732d2c0290e6dd1800e35ed
HEAD is now at 4310fe22 Add a Lisence comment

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git branch
  localdev
  master
* test123
  test456

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git diff ^test456
diff --git a/2.txt b/2.txt
deleted file mode 100644
index e69de29b..00000000

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git diff ^test456
diff --git a/2.txt b/2.txt
deleted file mode 100644
index e69de29b..00000000

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git checkout test456
Switched to branch 'test456'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ git diff ^test123
diff --git a/2.txt b/2.txt
new file mode 100644
index 00000000..e69de29b

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test456)
$ git checkout test123
Switched to branch 'test123'

85380@DESKTOP-3S1KOHB MINGW64 ~/Desktop/gittest/Telegram (test123)
$ git merge test456 --no-ff
Merge made by the 'recursive' strategy.
 2.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 2.txt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章