柚子快報邀請碼778899分享:linux git 使用
柚子快報邀請碼778899分享:linux git 使用
1. 下載git
yum -y install git
2. 配置用戶,郵箱
git config --global user.name 'tom'
git config --global user.email '328294199@qq.com'
git config --global color.ui true
git config --global --list
3. 初始化代碼目錄
mkdir -p /app/src/tom-app
cd /app/src/tom-app
git init
ll .git/
4. git工作空間
工作區(qū):工作目錄就是我們執(zhí)行命令git init時所在的地方
暫存區(qū):暫存區(qū)和本地倉庫都是在.git目錄,因為它們只是用來存數(shù)據(jù)的
本地倉庫:暫存區(qū)和本地倉庫都是在.git目錄,因為它們只是用來存數(shù)據(jù)的
遠程倉庫:遠程倉庫在中心服務器,也就是我們做好工作之后推送到遠程倉庫
5. git常用命令
命令 含義 git init 初始化本地倉庫目錄 git config --global 郵箱,用戶名,顏色 git add 提交數(shù)據(jù)到緩沖區(qū)(暫存區(qū)) git add . (所有文件) 或 git add 文件 git commit 把暫存區(qū)的數(shù)據(jù)提交到本地倉庫 git commit -m "標記/說明" git status 顯示工作空間的狀態(tài) git reset 回滾 git reset --soft cid(版本號) 把指定的版本數(shù)據(jù)內容下載到暫存區(qū) git reset HEAD 暫存區(qū) -->工作空間(被修改的狀態(tài)) git checkout 文件下載到工作空間并可以使用 git checkout . 或 git checkout 文件 git reset --mix 版本號 git reset --hard 版本號 把本地倉庫指定版本信息數(shù)據(jù)下載到工作目錄中 git branch 查看分支 git branch name 創(chuàng)建分支 git branch -d name 刪除分支 git checkout 分支名字 切換分支 git merge 分支名字 合并(吸收)分支(把指定的分支合并到當前分支中) git checkout -b name 創(chuàng)建分支并切換到這個分支 git pull origin 分支名字 用于從遠程倉庫獲取最新的提交,并將其合并到當前分支中, 它相當于執(zhí)行了 git fetch 后緊接著執(zhí)行了 git merge git fetch origin 分支名字 用于從遠程倉庫獲取最新的提交,但不會自動合并或更新本地分支 git push -u origin 分支名字 將本地倉庫代碼推送至遠程分支 git clone [https://|git] 用于初始化本地倉庫,只需要執(zhí)行一次。它會在本地創(chuàng)建一個新的目錄,并將遠程倉庫的整個代碼庫復制到該目錄中 git tag -a v1.0 -m "new v1.0" 給當前版本打個標簽,用于發(fā)版本 git push -u origin --tags 將標簽推送至遠程倉庫
#書寫代碼
]# echo 進度1% > index.html
#查看狀態(tài)
]# git status
# On branch master
# Initial commit
# Untracked files:
# (use "git add
# index.html
nothing added to commit but untracked files present (use "git add" to track)
#把工作區(qū)中未被跟蹤的文件加到暫存區(qū)進行跟蹤
git add .
#再次查看狀態(tài),文件已加入到暫存區(qū)
[root@gitlab tom-app]# git status
# On branch master
# Initial commit
# Changes to be committed:
# (use "git rm --cached
# new file: index.html
#把暫存區(qū)的文件提交到本地倉庫
[root@gitlab tom-app]# git commit -m "項目v1"
[master (root-commit) 92bbd3f] 項目v1
1 file changed, 1 insertion(+)
create mode 100644 index.html
#再次查看狀態(tài),工作區(qū)和暫存區(qū)都沒有文件要提交了
[root@gitlab tom-app]# git status
# On branch master
nothing to commit, working directory clean
#不小心把代碼刪除了,可以通過git checkout .把最新代碼從本地倉庫拉取下來
[root@gitlab tom-app]# rm -rf index.html
[root@gitlab tom-app]# ll
total 0
[root@gitlab tom-app]# git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm
# (use "git checkout --
#
# deleted: index.html
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gitlab tom-app]# git checkout .
[root@gitlab tom-app]# ll
total 4
-rw-r--r-- 1 root root 9 Jul 10 11:38 index.html
[root@gitlab tom-app]# git status
# On branch master
nothing to commit, working directory clean
6. 分支
#當前共一個分支,目前位于master分支
[root@gitlab tom-app]# git branch
* master
#從當前分支位置,創(chuàng)建要給新分支shopping
[root@gitlab tom-app]# git branch shopping
#當前倉庫共2個分支,代碼目前位于master分支
[root@gitlab tom-app]# git branch
* master
shopping
#切換代碼到shopping分支
[root@gitlab tom-app]# git checkout shopping
Switched to branch 'shopping'
#當前倉庫共2個分支,代碼目前位于shopping分支
[root@gitlab tom-app]# git branch
master
* shopping
#shopping分支新增代碼并提交---------------------------------------------------
[root@gitlab tom-app]# vim shopping.html
[root@gitlab tom-app]# git status
# On branch shopping
# Untracked files:
# (use "git add
#
# shopping.html
nothing added to commit but untracked files present (use "git add" to track)
[root@gitlab tom-app]# git add .
[root@gitlab tom-app]# git commit -m "shopping v1"
[shopping 94cead0] shopping v1
1 file changed, 1 insertion(+)
create mode 100644 shopping.html
#----------------------------------------------------------------------
#把shoping分支的代碼合并到master分支--------------------------------------
#先確保當前分支在master分支
[root@gitlab tom-app]# git checkout master
Switched to branch 'master'
[root@gitlab tom-app]# ll
-rw-r--r-- 1 root root 9 Jul 10 12:08 index.html
[root@gitlab tom-app]# git merge shopping
Updating 65d8fab..94cead0
Fast-forward
shopping.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 shopping.html
[root@gitlab tom-app]# ll
-rw-r--r-- 1 root root 9 Jul 10 12:08 index.html
-rw-r--r-- 1 root root 9 Jul 10 12:26 shopping.html
#----------------------------------------------------------------------
7. 遠程倉庫(gitee)
7.1. 方式一:https方式(密碼認證方式)
#倉庫為https(密碼認證方式),每次push的時候,都要輸入用戶名密碼
]
]# git push -u origin "master"
Username for 'https://gitee.com': 13202283227
Password for 'https://13202283227@gitee.com':
Counting objects: 12, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (12/12), 1.04 KiB | 0 bytes/s, done.
Total 12 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/a-little-phoenix/tomapp.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
7.2. 方式二:為了提交代碼方便,重新設置遠程倉庫地址為ssh的方式(密鑰認證方式)
git remote set-url origin git@gitee.com:a-little-phoenix/tomapp.git
#本地生成密鑰對
[root@gitlab tom-app]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9iVmjImoq30tv/FAixhelh9mBp/zAVWM8KvW4UF8Cy0 root@gitlab
The key's randomart image is:
+---[RSA 2048]----+
| ...+. |
| +... |
| . . E o |
| = = O . |
| . = @ S * . |
| . * B X B o |
| + ..B + . |
| . .o..+ |
|..o. oo.. |
+----[SHA256]-----+
[root@gitlab tom-app]# ll /root/.ssh/
total 12
-rw------- 1 root root 1679 Jul 10 13:45 id_rsa
-rw-r--r-- 1 root root 393 Jul 10 13:45 id_rsa.pub
gitee上面添加剛剛生成的公鑰
柚子快報邀請碼778899分享:linux git 使用
推薦閱讀
本文內容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯(lián)系刪除。