1. 程式人生 > >Git 初步學習

Git 初步學習

發現 rem pty pan testing ins 修改權限 -m 項目

學習目標

在linux 上安裝Git 服務器

在windows 上安裝 Git 客戶端

創建Git倉庫,git用戶

在windows 中獲取項目,對項目進行增刪改查,更新到服務器

創建兩個分支,進行分支修改和代碼合並

1. 在linux上安裝git服務器

  使用指令:sudo apt-get install git

  安裝完成效果:

stark@ubuntu:~$ git --version
git version 2.7.4 

2. 安裝Git客戶端,下載Git for Windows,安裝完成運行Git Bash 輸入指令

$ git --version
git version 2.15.0.windows.1

3. 服務器端創建Git 倉庫

stark@ubuntu:~/data/git$ mkdir gittest.git
stark@ubuntu:~/data/git$ git init gittest.git
Initialized empty Git repository in /home/stark/data/git/gittest.git/.git/

  以為git 默認禁止push代碼需要配置 .git/config 文件添加 (push失敗後,網上查詢)

[receive]
 denyCurrentBranch = ignore

  添加git用戶和git用戶組,並且修改權限

#創建用戶組
sudo groupadd gituser
#創建用戶
sudo useradd gituser -g gituser
sudo passwd gituser
sudo mkdir /home/gituser
sudo chown -R gituser /home/gituser
#修改git倉庫權限
sudo chown -R gituser.gituser gittest.git

4. 客戶端抓取項目,進行增刪改查

# 下載項目
$ git clone [email protected]:/home/stark/data/git/gittest.git
Cloning into ‘gittest‘...
[email protected]‘s password:
warning: You appear to have cloned an empty repository.
$ git init gittest/
Reinitialized existing Git repository in D:/GitTest/gittest/.git/
#創建文件,上傳
$ echo "hellohit">hello.txt
$ git add hello.txt
$ git commit -m ‘hello‘
$ git push origin master
#修改文件,上傳
$ echo ‘hellogit‘ >hello.txt
$ git add hello.txt
$ git commit -m ‘modify‘
$ git push origin master
#在另一處確認修改
$ git pull
#刪除文件
$ rm hello.txt
$ git rm hello.txt
$ git commit -m ‘remove‘
$ git push origin master
#查詢日誌 關鍵信息是commit id
$ git log
#回滾到某一版本(本地版本)
$ git reset –hard <commit id>

  在倉庫下沒有發現上床的文件,因為git倉庫保存的是快照。在另外一處重新抓取項目,可以發現文件已經被上傳了

5. 使用Git的分支功能

#添加一個空白文件
$ touch branch.txt
$ git add status
$ git commit -m ‘add file‘
$ git push origin master
#創建分支
$ git branch testing1
$ git branch testing2
#查看本地分支
$ git branch
#切換到分支進行文件修改,push到遠程分支
$ git checkout testing1
$ echo ‘testing1‘> branch.txt
$ git add branch.txt
$ git commit -m ‘testing1‘
$ git push origin testing1

$ git checkout testing2
$ echo ‘testing2‘> branch.txt
$ git add branch.txt
$ git commit -m ‘testing2‘
$ git push origin testing2
#查看遠程分支
$ git branch -r
#合並分支1,上傳,刪除分支
$ git checkout master
$ git merge testing1 # Fast-forward 表示沒有沖突
$ git push origin master
$ git push origin --delete testing1
$ git branch -d testing1 
#合並分支2,解決沖突,上傳刪除分支
$ git merge testing2  #CONFLICT 表示合並出現沖突
#解決沖突後像修改文件一樣上傳就行

Git 初步學習