git使用配置說明

在單機上使用git

安裝git服務,可以使用yum直接進行安裝

 yum install -y git
[root@nfs2 data]# yum install -y git
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00     
elrepo | 2.9 kB 00:00:00     
epel/x86_64/metalink | 8.4 kB 00:00:00     
extras | 3.4 kB 00:00:00     
mongodb-org-4.0 | 2.5 kB 00:00:00     
updates  
---------------省略
Dependency Updated:
  perl-Git.noarch 0:1.8.3.1-14.el7_5                                                                                                                                           
​
Complete!

安裝完成後,再來初始化git服務及創建項目,創建一個項目測試文件

[root@nfs2 /]# mkdir /data/github
[root@nfs2 /]# cd /data/github/
[root@nfs2 github]# git init
Initialized empty Git repository in /data/github/.git/
[root@nfs2 github]# echo -e "123456list" > 1.txt

向版本管理中添加個項目
並將這個版本上傳至管理中心
git add 版本文件
git commit -m "add 版本文件"
初次執行代碼上傳會提示添加個聯繫人郵件地址,按照提示的運行  git config --global user.email 及以下的命令,指定郵件和聯繫人名字即可。如:

[root@nfs2 github]# git add 1.txt 
[root@nfs2 github]# git commit -m "add 1.txt"
​
*** 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 'root@nfs2.(none)')
[root@nfs2 github]# git config --global user.email "[email protected]"
[root@nfs2 github]# git config --global user.name "list"

向版本管理中提交代碼版本
這次就順利的提交了一個版本的文件。過程如下:

[root@nfs2 github]# git add 1.txt 
[root@nfs2 github]# git commit -m "add 1.txt"
[master (root-commit) e23962e] add 1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt

查看git版本更新信息
多做幾次文件修改並提交版本更新,然後使用git log查看版本更新記錄信息
git log查看信息要素較多,可以添加選項--pretty=oneline,讓其只顯示一行。
git log --pretty=oneline 
這裏測試過程如下:

[root@nfs2 github]# echo "123" >> 1.txt
[root@nfs2 github]# git add 1.txt 
[root@nfs2 github]# git commit -m "add 1.txt"
[master 1812cdc] add 1.txt
 1 file changed, 1 insertion(+)
[root@nfs2 github]# git log               ---------------git log顯示的內容
commit 1812cdc1c348887f1b97342e355ae9a02a58902e
Author: list <[email protected]>
Date: Fri Nov 23 14:41:29 2018 +0800
​
    add 1.txt
​
commit e23962e2d73df6d271b7def7a850424d4a1ef795
Author: list <[email protected]>
Date: Fri Nov 23 14:30:49 2018 +0800
​
    add 1.txt
[root@nfs2 github]# git log --pretty=oneline        -------------git log --pretty=oneline顯示的內容
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt

git更新的歷史版本管理
退回到某個版本
git reset --hard  版本ID
退回到某個版本後,如果再使用git log這個執行命令查看的話會發現最新版本不再顯示在記錄當中。這時候就需要使用git reflog查看所有的歷史版本記錄

[root@nfs2 github]# git log --pretty=oneline
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt
[root@nfs2 github]# git reset --hard e23962e2d
HEAD is now at e23962e add 1.txt
[root@nfs2 github]# git log --pretty=oneline
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt

再次將新版本提交,並使用git reflog查看所有的版本操作記錄

[root@nfs2 github]# git reset --hard 1812cdc
HEAD is now at 1812cdc add 1.txt
[root@nfs2 github]# git reflog
1812cdc HEAD@{0}: reset: moving to 1812cdc
e23962e HEAD@{1}: reset: moving to e23962e2d
1812cdc HEAD@{2}: commit: add 1.txt
e23962e HEAD@{3}: commit (initial): add 1.txt

將版本中修改過的文件,這些文件只進行了add沒有執行commit操作,讓其退回到上一次提交的狀態,使用git reset HEAD files,然後再執行
git checkout -- files  的操作
修改下1.txt文件,向文件內追加一行內容,然後執行git add做準備提交更新的操作。注意:不要執行git commit提交上去!恢復後文件中新追加的一行內容不存在了。測試過程如下:

[root@nfs2 github]# git reset --hard 1812cdc
HEAD is now at 1812cdc add 1.txt
[root@nfs2 github]# git reflog
1812cdc HEAD@{0}: reset: moving to 1812cdc
e23962e HEAD@{1}: reset: moving to e23962e2d
1812cdc HEAD@{2}: commit: add 1.txt
e23962e HEAD@{3}: commit (initial): add 1.txt

撤銷更改
如果操作過程中不小心刪除了版本中的某個文件,撤銷刪除使用

git chckout -- files 
測試過程如下:
[root@nfs2 github]# rm -rf 1.txt 
[root@nfs2 github]# git checkout -- 1.txt
[root@nfs2 github]# ls
1.txt
[root@nfs2 github]# cat 1.txt 
123456list
123

刪除和恢復git某個版本
git commit -m "delete 文件"
創建個測試文件2.txt,文件內容爲1234
首先刪除掉2.txt本地文件,再刪除掉git中的版本文件

[root@nfs2 github]# git rm 2.txt 
rm '2.txt'
[root@nfs2 github]# ls             ----------查看生效的文件
1.txt
[root@nfs2 github]# git log --pretty=oneline             ------在對git刪除前先查看git中的記錄
7a659af5d27ffb25baba2f5d5199a462db503a0d add 2.txt
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt
[root@nfs2 github]# git commit -m "delete 2.txt"       --------刪除git中的2.txt版本記錄
[master 5efa36d] delete 2.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 2.txt
[root@nfs2 github]# git log --pretty=oneline           --------查看這個版本記錄的狀態
5efa36d5e86aa9821e9c6772828cc9a9ca31e250 delete 2.txt
7a659af5d27ffb25baba2f5d5199a462db503a0d add 2.txt
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt

恢復git內刪除的版本文件
git reset --hard  版本ID
首先查看刪除2.txt後的現在git狀態,恢復後再次查看git的狀態信息。過程操作如下:

[root@nfs2 github]# git log --pretty=oneline 
5efa36d5e86aa9821e9c6772828cc9a9ca31e250 delete 2.txt
7a659af5d27ffb25baba2f5d5199a462db503a0d add 2.txt
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt
[root@nfs2 github]# git reset --hard 7a659af       ---------hard 恢復刪除掉的版本的ID,將其刪除操作撤銷掉
HEAD is now at 7a659af add 2.txt
[root@nfs2 github]# git log --pretty=oneline       ---------查看恢復後的git版本狀態信息
7a659af5d27ffb25baba2f5d5199a462db503a0d add 2.txt
1812cdc1c348887f1b97342e355ae9a02a58902e add 1.txt
e23962e2d73df6d271b7def7a850424d4a1ef795 add 1.txt

建立遠程倉庫

使用github倉庫來管理代碼版本
首先在github中創建一個新的項目
git使用配置說明
創建好項目後還需要給github訪問服務器的密鑰配置
以下是在github中添加服務器密鑰的過程。過程如下:
git使用配置說明
點擊頭像的Settings,進入後再尋找到SSH and GPG keys的點擊入口
git使用配置說明
這步先要在服務器上生成密鑰對,然後把公鑰內容保存到github中

[root@nfs1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
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:
03:c5:c8:44:b8:60:b5:c2:fc:d2:3a:cb:2d:bb:a3:70 root@nfs1
The key's randomart image is:
+--[ RSA 2048]----+
|   ..=oo.        |
| oo ..o..        |
| .+....          |
|   +.  .         |
|  . o   S        |
|   o     .       |
|. E              |
|.oo+             |
|..==.            |
+-----------------+
[root@nfs1 ~]# 
[root@nfs1 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWskJGS7f+p/KM8J3gelehgDTR19Pj6L6umZSGusJcyLzFwD3n92sbh5q4NwKnB/TYiGonZPY783CKl738A7h8CVy48HHtTyo4J9VQOFwxwD+aLC7qtHqHLDf2ypVLMCiP1kH8d0A9CN2lcG1wSTjIr4ZX5wEy8QY3uUtM9A7K4smJ/ZgZDM58c56z+bqn7j6lD5xDnCxrj/FtIuEcU2BrDju03WVAVwQXbKgzWfcQ8GzZgJeWQ0lKDTL3Eo7kbOOPFaxN3TnIdehDZnlmskJMlAnhavWhUG6Anx3a93homi0dSJEl3HRWClzZ3y+yseZ6Domb+vgEFOiTLUqU8KQ5 root@nfs1

然後在github中保存公鑰的內容
git使用配置說明

到此步驟設置github完成
git使用配置說明
在服務器端/客戶端使用版本上傳等操作
在服務/用戶端初始化github應用,在github上選擇ssh模式的git方式
git使用配置說明

分步驟執行一,在項目目錄下創建一個初始文件並添加到版本管理中
git使用配置說明

echo "# monice" >> README.md 
git init 
git add README.md 
git commit -m "first commit"
github的初始化過程操作如下:
[root@nfs1 monice]# echo "# monice" >> README.md 
[root@nfs1 monice]# git init 
Initialized empty Git repository in /usr/local/src/monice/.git/
[root@nfs1 monice]# git add README.md 
[root@nfs1 monice]# git commit -m "new ceshi"
[master (root-commit) bb896e0] new ceshi
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

分步執行二,指定github上的項目,github項目名稱必須要和當前所在的項目目錄名稱相同,對應github中的項目名來修改用戶端的項目名稱
初次github上傳會提示輸入用戶名和密碼。這裏的用戶名和密碼是github的用戶名及密碼
如果git使用的是SSH模式,則在git push時則不需要驗證密碼。
操作過程如下:
git使用配置說明

[root@nfs1 monice]# git remote add origin [email protected]:hanxiang233/monice.git
[root@nfs1 monice]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 217 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/hanxiang233/monice/pull/new/master
remote: 
To [email protected]:hanxiang233/monice.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

再次向github中更新幾個腳本文件
再次上傳代碼到github中則不再需要驗證github用戶名和密碼了,git更新過程如下
首先add項目文件到本地版本管理中

[root@nfs1 monice]# git add 1.sh fo
for.sh   fors.sh  foss.sh  
[root@nfs1 monice]# git add 1.sh for*
[root@nfs1 monice]# git commit -m "new shell"
[master 2390d8e] new shell
 3 files changed, 32 insertions(+)
 create mode 100644 1.sh
 create mode 100644 for.sh
 create mode 100644 fors.sh
[root@nfs1 monice]# git push
Counting objects: 6, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 641 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To [email protected]:hanxiang233/monice.git
   5ac93e9..2390d8e  master -> master

出現的問題
<br/>我在執行git add *.sh和git commit -m "new ceshi"<br/>執行git commit -m提示出(表示已經提交了一次,之前提交的版本內容沒有git push):<br/>

[root@nfs1 monice]# git push
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
然而就在我執行git push的時候又有信息提示,這次是警告信息:
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
​
  git config --global push.default matching
​
To squelch this message and adopt the new behavior now, use:
​
  git config --global push.default simple
​
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

安照提示我執行了git config --global push.default simple這句指令,然後重新git push。重新push會提示重新輸入github的密碼進行驗證

[root@nfs1 monice]# git push
Username for 'https://github.com': hanxiang233
Password for 'https://[email protected]': 
Counting objects: 12, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1.78 KiB | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To https://github.com/hanxiang233/monice.git
   bb896e0..e7da5d5 master -> master

這次也成功的github中更新的版本內容:
git使用配置說明

再次完整測試
在項目目錄下再創建一個2.txt文件進行上傳到github中測試
這裏還得注意的是,上傳如果使用的是https方式,所以在每次push版本時都需要驗證github的密碼。
如果使用SSH初始化git時,再次執行git上傳代碼則不需要驗證github的用戶名和密碼

[root@nfs1 monice]# echo "1122334455" > 2.txt
[root@nfs1 monice]# git add 2.txt 
[root@nfs1 monice]# git commit -m "add 2.txt"
[master 9b6d416] add 2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 2.txt
[root@nfs1 monice]# git push
Username for 'https://github.com': hanxiang233
Password for 'https://[email protected]': 
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/hanxiang233/monice.git
   e7da5d5..9b6d416  master -> master
   
   ----------------------------push 3.txt版本更新文件
[root@nfs1 monice]# git push
Username for 'https://github.com': hanxiang233
Password for 'https://[email protected]': 
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/hanxiang233/monice.git
   9b6d416..bc1cadd  master -> master

在github上查看push的文件,可以看到文件已經被上傳至github上
git使用配置說明

克隆遠程倉庫

在github上隨意找到一個其他人的開源項目,複製ssh的git地址,然後在自己本地服務器上git克隆下來整個項目,具體操作過程如下:
隨意在本地創建一個目錄,然後git  clone下載一個項目

[root@nfs1 src]# mkdir ceshi
[root@nfs1 src]# cd ceshi/
[root@nfs1 ceshi]# ls

git clone開源項目,將項目下載至本地,然後查看當前下載的項目文件內容

[root@nfs1 ceshi]# git clone [email protected]:DIYgod/diygod.me.git
Cloning into 'diygod.me'...
remote: Enumerating objects: 223, done.
remote: Counting objects: 100% (223/223), done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 1658 (delta 98), reused 217 (delta 98), pack-reused 1435
Receiving objects: 100% (1658/1658), 19.48 MiB | 36.00 KiB/s, done.
Resolving deltas: 100% (745/745), done. 
```   
github下來的開源項目文件

[root@nfs1 ceshi]# ls
diygod.me
[root@nfs1 ceshi]# cd diygod.me/
[root@nfs1 diygod.me]# ls
_config.yml package.json README.md scaffolds source themes yarn.lock

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章