獲取git倉庫兩種方式:
(1)本地創建
git init
git add *.c
git add LICENCE
git commmit -m "new git respository"
(2)克隆已存在的倉庫
git clone <url>
記錄變更到倉庫中:
在工作目錄中,每個文件只能存在兩種狀態:tracked和untracked。tracked狀態包裝modified, unmodifed和staged.
(3)檢查文件狀態
git status
git status -s
Tracking新文件
(4)git add newFile
Staging已修改文件
(5)git add modifiedFile
忽略文件
編輯.gitignore文件
提交修改
(6)git commit -m "comment"
跳過staging狀態
(7)git commit -a -m "comment"
移除文件
從git中移除文件,首先從tracked文件中然後提交它。
(8)git rm <filename>
在git中重命名文件
(9)git mv oldfile newfile
-------------------------------------------------------------------------------------------------
查閱提交歷史:
(10)git log
-p --patch: 顯示每次提交引入的差異。
git log -p -2: 顯示最後兩次提交差異內容
--pretty: 格式化的內容顯示,可選的格式爲oneline, short, full, fuller
format選項
git log --pretty=format=:"%h - %an, %ar : %s"
(11)限制日誌輸出
git log --since=2.weeks
-S: 用於過濾,只顯示匹配的內容
放棄修改
(12)git commit -amend ##一般用在提交內容時少提交了某些內容,想重新提交;
Unstage文件
(13)git reset HEAD <stagedFile>
將已修改文件還原
(14)git checkout -- <modifiedFile>
--------------------------------------------------------------------------------------------------
遠程管理
顯示配置的遠程服務器
git remote //顯示遠程指定的句柄
git remote -v //顯示URL
新增遠程倉庫
git remote add pb https://github.com/paulboone/ticgit
遠程拉取
git fetch <remote>
推送remote
git push <remote> <branch> //推送master分支到origin服務器
探查遠程
git remote show origin
對remote重命名
git remote rename pb paul
git remote rm
---------------------------------------------------------------------------------------------------
標籤:
列出標籤:
git tag
git tag -l <regexpr>
創建標籤
git tag -a v1.4 -m "my version 1.4" //-a:標籤名 -m:標籤消息
git show <tag>
分享標籤
git push origin <tagname>
刪除標籤
git tag -d <tag>
checkout標籤
git checkout 2.0.0
---------------------------------------------------------------------------------------------------
分支:
git中默認名稱是master,當你開始提交修改,沒提交一次,master向前移動一次;
創建分支:
git branch newBranch
切換分支:
git checkout <branch>
基礎分支與合併
基礎分支master上已經commit幾次
git checkout -b iss53 //創建新分支並切換到新分支iss53上
git merge hotfix //合併hotfix分支到當前分支上
刪除分支:
git branch -d branchName
衝突合併:
在合併的過程中發現文件存在衝突
git status
手動解決衝突或者使用git mergetool圖形化工具解決衝突
--
分支管理
列出當前分支
git branch
-v :顯示最後提交的註釋
--merged:顯示已合併的分支
--no-merged:
分支工作流:
主題分支:
主題分支在任何規模的項目上都可用,topic-branch存在時間短暫,主要是爲特殊的功能存在;
遠程分支:
遠程引用是指向遠程倉庫中分支、標籤。
git ls-remote [remote] 或者 git remote show [remote] //獲取遠程引用
遠程跟蹤分支名稱的形式如[remote]/[branch]
推送:
git push <remote> <branch>
git push origin serverfix:awesomebranch //本地推送到遠程awesomebranch分支上
拉取:
相比git pull而言,git fetch && git merge更推薦使用
刪除遠程分支
git push <remote> --delete <branch>
---------------------------------------------------------------------------------------------------
git中存在兩種方式將一個分支合併到另一個分支上,分別是merge和rebase
git checkout experimental //
git rebase master
---------------------------------------------------------------------------------------------------
在服務器上使用git
在服務器上運行git
1) 將已有的倉庫導出到新的空倉庫中。爲了在克隆倉庫過程中創建一個新的倉庫,使用--bare選項
git clone --bare my_project my_project.git
cp -Rf my_project/.git my_project.git
2) 將倉庫放置到服務器上
scp -r my_project [email protected]:/srv/git
3) 克隆到本地
git clone [email protected]:/srv/git/my_project.git
在my_project.git目錄中執行
git init --bare --shared
4) 生成ssh公鑰
cd ~/.ssh 列出目錄下的文件,以.pub結尾的文件是公鑰文件
ssh-keygen -o
添加到服務器~/.ssh/authorized_keys文件中
5) 啓動服務器
# adduser git
# su git
# cd
# mkdir .ssh && chmod 700 .ssh
# touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
# cat ~/xxx_ras.pub >> ./ssh/authorized_keys
# cd /srv/git
# mkdir project.git
# cd project.git
# git init --bare
6) git守護進程
$ git daemon --reuseaddr --base-path=/srv/git /srv/git
創建git-daemon.service文件,添加如下內容
------------------------------------------------------------------
[Unit]
Description=Start Git Daemon
[Service]
ExecStart=/usr/bin/git daemon --reuseaddr --base-path=/srv/git /srv/git
Restart=always
RestartSec=500ms
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=git-daemon
User=git
Group=git
[Install]
WantedBy=multi-user.target
------------------------------------------------------------------
移動到/etc/systemd/system目錄中
# systemctl enable git-daemon
---------------------------------------------------------------------------------------------------