development:system:git
This is an old revision of the document!
Table of Contents
Git
Cài đặt và cấu hình
$ apt-get install git $ git config --global user.name "TungNT" $ git config --global user.email "tungnt.blue@gmail.com"
Tạo SSH Key
$ ssh-keygen -t rsa -b 4096 -C "tungnt.blue@gmail.com"
Tạo nhiều tài khoản ssh key
$ ssh-keygen -t rsa -b 4096 -C “tungnt.blue@gmail.com” Generating public/private rsa key pair. Enter file in which to save the key (/var/root/.ssh/id_rsa): id_rsa_tungntblue Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in id_rsa_tungntblue. Your public key has been saved in id_rsa_tungntblue.pub. $ ssh-keygen -t rsa -b 4096 -C “nguyentung@mobgame.vn” Generating public/private rsa key pair. Enter file in which to save the key (/var/root/.ssh/id_rsa): id_rsa_nguyentung Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in id_rsa_nguyentung. Your public key has been saved in id_rsa_nguyentung.pub. The key fingerprint is: $ ls -la /var/root/.ssh/ -rw------- 1 root wheel 3243 Nov 1 10:26 id_rsa -rw-r--r-- 1 root wheel 747 Nov 1 10:26 id_rsa.pub -rw------- 1 root wheel 3243 Nov 1 22:24 id_rsa_nguyentung -rw-r--r-- 1 root wheel 753 Nov 1 22:24 id_rsa_nguyentung.pub -rw------- 1 root wheel 3243 Nov 1 22:21 id_rsa_tungntblue -rw-r--r-- 1 root wheel 753 Nov 1 22:21 id_rsa_tungntblue.pub -rw-r--r-- 1 root wheel 5036 Nov 1 10:23 known_hosts
Add các key:
$ ssh-add ~/.ssh/id_rsa_tungntblue Identity added: /var/root/.ssh/id_rsa_tungntblue (/var/root/.ssh/id_rsa_tungntblue) $ ssh-add ~/.ssh/id_rsa_nguyentung Identity added: /var/root/.ssh/id_rsa_nguyentung (/var/root/.ssh/id_rsa_nguyentung) $ ssh-add -l 4096 SHA256:HntA0FUgaD0ELTaHSU/h0SfTAKVPkJ9xEjnP6aXyCvM /var/root/.ssh/id_rsa_tungntblue (RSA) 4096 SHA256:2cJDMSnLCJPnd6K4cIdjmI58FwJ/X7u4Wrml4S8ulHw /var/root/.ssh/id_rsa_nguyentung (RSA)
Tạo command add key:
$ vim gi.sh #! /bin/bash ssh-add /var/root/.ssh/id_rsa_tungntblue ssh-add /var/root/.ssh/id_rsa_tungnt9pay ssh-add /var/root/.ssh/id_rsa_nguyentung ssh-add -l $ chmod +x gi.sh $ ln -s gi.sh /usr/local/bin/gi $ gi
Cấu hình Host sử dụng key:
$ vim ~/.ssh/config Host bitbucket.org HostName bitbucket.org PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_tungntblue Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_tungntblue Host git.mobgame.mobi HostName mobgame.mobi PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_nguyentung
Khởi tạo Git
$ git init $ git add . $ git commit -am 'Init source' $ git remote add origin git@github.com:abc/xyz.git $ git remote -v $ git push origin master
Branch
$ git checkout -b dev $ git checkout -b dev master $ git branch -D dev $ git checkout [branch_name] -- [file_path] $ git diff [branch_1] [branch_2] $ git diff --name-only [branch_1] [branch_2] $ git diff [branch_1] [branch_2] --file $ git diff --name-only master release/complete > diff_master_with_release_complete.txt
Xem log
$ git reflog $ git reflog master
Sửa commit message cuối cùng
$ git commit --amend $ git reset --hard [commit_version]
Submodule
git submodule add {GIT_URL} {DIR_PATH}
GitFlow
https://github.com/1sitevn/gitflow
https://nvie.com/posts/a-successful-git-branching-model
tungnt@MacBook-Pro-cua-Nguyen-2 1site % mkdir gitflow tungnt@MacBook-Pro-cua-Nguyen-2 1site % cd gitflow tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % ls -la total 0 drwxr-xr-x 2 tungnt staff 64 Aug 12 16:14 . drwxr-xr-x@ 7 tungnt staff 224 Aug 12 16:14 .. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % echo "# gitflow" >> README.md tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git init Initialized empty Git repository in /private/var/www/1site/gitflow/.git/ tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git add README.md tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git commit -m "first commit" [master (root-commit) 0f7d8f6] first commit 1 file changed, 1 insertion(+) create mode 100644 README.md tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git branch -M main tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git remote add origin git@github.com:1sitevn/gitflow.git tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push -u origin main Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 290 bytes | 290.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:1sitevn/gitflow.git * [new branch] main -> main branch 'main' set up to track 'origin/main'. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git branch develop tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git branch develop * main tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout develop Switched to branch 'develop' tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push -u origin develop Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'develop' on GitHub by visiting: remote: https://github.com/1sitevn/gitflow/pull/new/develop remote: To github.com:1sitevn/gitflow.git * [new branch] develop -> develop branch 'develop' set up to track 'origin/develop'. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout -b feature/1-add-test-file develop Switched to a new branch 'feature/1-add-test-file' tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git status On branch feature/1-add-test-file Untracked files: (use "git add <file>..." to include in what will be committed) test.js nothing added to commit but untracked files present (use "git add" to track) tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git add test.js tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git commit -m '#1 - add a new test.js file' [feature/1-add-test-file c637c90] #1 - add a new test.js file 1 file changed, 1 insertion(+) create mode 100644 test.js tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push fatal: The current branch feature/1-add-test-file has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin feature/1-add-test-file To have this happen automatically for branches without a tracking upstream, see 'push.autoSetupRemote' in 'git help config'. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push --set-upstream origin feature/1-add-test-file Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 399 bytes | 399.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'feature/1-add-test-file' on GitHub by visiting: remote: https://github.com/1sitevn/gitflow/pull/new/feature/1-add-test-file remote: To github.com:1sitevn/gitflow.git * [new branch] feature/1-add-test-file -> feature/1-add-test-file branch 'feature/1-add-test-file' set up to track 'origin/feature/1-add-test-file'. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout develop Switched to branch 'develop' Your branch is up to date with 'origin/develop'. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git pull remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (1/1), 906 bytes | 906.00 KiB/s, done. From github.com:1sitevn/gitflow 0f7d8f6..e94388a develop -> origin/develop Updating 0f7d8f6..e94388a Fast-forward test.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.js tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout release-1.0.0 develop error: pathspec 'release-1.0.0' did not match any file(s) known to git error: pathspec 'develop' did not match any file(s) known to git tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout -b release-1.0.0 develop Switched to a new branch 'release-1.0.0' tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git tag 'v1.0.0' tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push --tags Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:1sitevn/gitflow.git * [new tag] v1.0.0 -> v1.0.0 tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git merge develop Already up to date. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git branch develop feature/1-add-test-file main * release-1.0.0 tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push origin release-1.0.0 Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'release-1.0.0' on GitHub by visiting: remote: https://github.com/1sitevn/gitflow/pull/new/release-1.0.0 remote: To github.com:1sitevn/gitflow.git * [new branch] release-1.0.0 -> release-1.0.0 tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % ls -la total 8 drwxr-xr-x 4 tungnt staff 128 Aug 12 16:43 . drwxr-xr-x@ 7 tungnt staff 224 Aug 12 16:14 .. drwxr-xr-x 14 tungnt staff 448 Aug 12 16:43 .git -rw-r--r-- 1 tungnt staff 10 Aug 12 16:15 README.md tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git pull origin main remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (1/1), 890 bytes | 445.00 KiB/s, done. From github.com:1sitevn/gitflow * branch main -> FETCH_HEAD 0f7d8f6..9cd0401 main -> origin/main Updating 0f7d8f6..9cd0401 Fast-forward test.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.js tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % ls -la total 16 drwxr-xr-x 5 tungnt staff 160 Aug 12 16:44 . drwxr-xr-x@ 7 tungnt staff 224 Aug 12 16:14 .. drwxr-xr-x 14 tungnt staff 448 Aug 12 16:44 .git -rw-r--r-- 1 tungnt staff 10 Aug 12 16:15 README.md -rw-r--r-- 1 tungnt staff 20 Aug 12 16:44 test.js tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git tag 'v1.0.0' fatal: tag 'v1.0.0' already exists tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push --tags Everything up-to-date tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git branch develop feature/1-add-test-file * main release-1.0.0 tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git branch -d release-1.0.0 Deleted branch release-1.0.0 (was e94388a). tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push origin -d release-1.0.0 To github.com:1sitevn/gitflow.git - [deleted] release-1.0.0 tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git branch -d feature/1-add-test-file Deleted branch feature/1-add-test-file (was c637c90). tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push origin -d feature/1-add-test-file To github.com:1sitevn/gitflow.git - [deleted] feature/1-add-test-file tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout -b hotfixs main Switched to a new branch 'hotfixs' tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git add test.js tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git commit -m '#1 - Hotfix test' [hotfixs c16aab1] #1 - Hotfix test 1 file changed, 1 insertion(+), 1 deletion(-) tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push origin hotfixs Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 395 bytes | 395.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'hotfixs' on GitHub by visiting: remote: https://github.com/1sitevn/gitflow/pull/new/hotfixs remote: To github.com:1sitevn/gitflow.git * [new branch] hotfixs -> hotfixs tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git branch -D hotfixs Deleted branch hotfixs (was c16aab1). tungnt@MacBook-Pro-cua-Nguyen-2 gitflow % git push origin -d hotfixs To github.com:1sitevn/gitflow.git - [deleted] hotfixs
Git Rebase & Git Merge
Một số lỗi có thể gặp
"Warning: the ECDSA host key for 'github.com' differs from the key for the IP address" issue
development/system/git.1723470283.txt.gz · Last modified: 2024/08/12 13:44 by tungnt