git介紹
與svn不同的是,svn是集中式管理,當自己主機上修改了檔案,必須送出到伺服器,其他人才能送出,不然沖突
git是自己本機也可以作為倉庫,也有當做伺服器,分布式管理
安裝:
bash-completion:tab鍵補全指令
[root@centos7-2 ~]# sudo yum install git bash-completion
1、配置個人資訊
[root@centos7-2 ~]# git config --global user.name "jacker"
[root@centos7-2 ~]# git config --global user.email "[email protected]"
2、檢視使用者名和email
[root@centos7-2 ~]# cat /root/.gitconfig
[user]
name = jacker
email = [email protected]
3、建立倉庫推送檔案
[root@centos7-2 ~]# mkdir /home/gitroot
[root@centos7-2 ~]# cd /home/gitroot/
初始化操作
[root@centos7-2 gitroot]# git init
Initialized empty Git repository in /home/gitroot/.git/
[root@centos7-2 gitroot]# vim test.txt
[root@centos7-2 gitroot]# git add test.txt
[root@centos7-2 gitroot]# git commit -m "add a test file test.txt"
[master (root-commit) ee26be4] add a test file test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
當更新檔案時,沒有送出,會提示你add,commit
[root@centos7-2 gitroot]# git status
#On branch master
#Changes not staged for commit:
#(use "git add <file>..." to update what will be committed)
#(use "git checkout -- <file>..." to discard changes in working directory)
#
#modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
2、不送出,并清除更改的内容
git checkout -- test.txt
3、[root@centos7-2 gitroot]# git status
nothing to commit, working directory clean
4、隻更改不送出
[root@centos7-2 gitroot]# vim test.txt
[root@centos7-2 gitroot]# git add test.txt
以下是将test.txt給送出了,更新到最新的版本
[root@centos7-2 gitroot]# git reset HEAD test.txt
[root@centos7-2 gitroot]# cat test.txt
1.test1
2.test2
3.test3
4.test4
[root@centos7-2 gitroot]# git log

簡寫:
[root@centos7-2 gitroot]# git log --pretty=oneline
2b01244ccecfc72be09c8314d4050a023d4f8a32 6.txt
6b93439a630a9992eda8355bd421a23a3d90a269 5.txt
94eac572484320bfe534d92013be17c147907ef5 add a test file test.txt
ee26be4b4f753d09741b3685224d00902234936e add a test file test.txt
恢複至哪個版本--git reset
[root@centos7-2 gitroot]# git reset --hard 94eac
HEAD is now at 94eac57 add a test file test.txt
恢複到哪個版本後,目前版本就是該版本
檔案的删除操作
[root@centos7-2 gitroot]# git rm test.txt
[root@centos7-2 gitroot]# git commit -m 'rm test.txt'
GitHub官網:github.com
新增賬號并激活,然後開始建立主機的倉庫!
建立完成後,添加key:
點選浏覽器右上角頭像——setting——SSH and GPG keys(選擇SSH keys)——在伺服器(虛拟機)執行ssh-keygen指令生成密鑰對(/root/.ssh/id_rsa-私鑰, /root/.ssh/id_rsa.pub-公鑰)——将公鑰複制到浏覽器後點“添加”。
以下是輸入github的密碼
新增資源
檢視
[root@centos7-2 home]# git clone [email protected]:sundysj/jack-git.git
編輯
[root@centos7-2 jack-git]# vim test1.txt
[root@centos7-2 jack-git]# git add test1.txt
[root@centos7-2 jack-git]# git commit -m 'ad test1'
[master 7c80b77] ad test1
1 file changed, 4 insertions(+)
create mode 100644 test1.txt
#送出到遠端git push
[root@centos7-2 jack-git]# 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)
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 271 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:sundysj/jack-git.git
430a41c..7c80b77 master -> master
現在在github遠端上建立一個檔案
在用戶端上拉去最新的檔案
[root@centos7-2 jack-git]# git pull origin master
*:表示目前的所在分支
檢視分支
[root@centos7-2 jack-git]# git branch
master
建立分支
[root@centos7-2 jack-git]# git branch
jack
切換分支
[root@centos7-2 jack-git]# git checkout jack
Switched to branch 'jack'
jack分支上建立檔案
[root@centos7-2 jack-git]# vim testjack1.txt
[root@centos7-2 jack-git]# git add testjack1.txt
[root@centos7-2 jack-git]# git commit -m 'ad tet'
将這個分支推送到遠端github上
[root@centos7-2 jack-git]# git push --set-upstream origin jack
檢視github上的分支
1、先切換到master上
[root@centos7-2 jack-git]# git checkout master
2、合并
[root@centos7-2 jack-git]# git merge jack
3、合并原則
主分支master不變,開發人員就在dev分支上開發後合并到master分支上
[root@centos7-2 jack-git]# git branch -d jack
warning: not deleting branch 'jack' that is not yet merged to
'refs/remotes/origin/jack', even though it is merged to HEAD.
error: The branch 'jack' is not fully merged.
If you are sure you want to delete it, run 'git branch -D jack'.
說明: -d:删除;-D:強制删除
[root@centos7-2 jack-git]# git branch -D jack
Deleted branch jack (was 06beb7b).
You have new mail in /var/spool/mail/root
master分支是非常重要的,線上釋出代碼用這個分支,平時開發代碼不要在該分支操作;
建立dev分支,專門用作開發,隻有到釋出到線上之前再把dev合并到master上
開發人員應該在dev分支的基礎上再分支成個人分支,自己的分支(在自己的pc上)裡面開發代碼,然後合并到dev上
場景:
當你正在進行項目中某一部分的工作,裡面的東西處于一個比較雜亂的狀态,而你想轉到其他分支上進行一些工作中。問題是你不想送出進行了一半的工作,否則以後你無法回到這個工作點。解決問題的辦法就是:git stash(存儲)指令。
比如,我們在jack分支,編輯一個新的檔案3.txt,此時我們要去其他分支。
[root@centos7-2 jack-git]# vim 3.txt
[root@centos7-2 jack-git]# git add 3.txt
#此時我不想送出,想切換到其他分支去看
[root@centos7-2 jack-git]# git status
#無檔案要送出,幹淨的工作區
但是你發現3.txt不見了?
[root@centos7-2 jack-git]# ls
README.md test1.txt test2 test3 testjack1.txt
找出來:
[root@centos7-2 jack-git]# git stash list
stash@{0}: WIP on master: 1f6aec8 ch te
[root@centos7-2 jack-git]# git stash apply stash@{0}
3.txt README.md test1.txt test2 test3 testjack1.txt
删除儲存的内容:git stash drop stash@{0}
遠端上建立分支
用戶端上更新
[root@centos7-2 jack-git]# git pull origin test
檢視遠端倉庫資訊
[root@centos7-2 jack-git]# git remote -v
origin [email protected]:sundysj/jack-git.git (fetch)
origin [email protected]:sundysj/jack-git.git (push)
檢視遠端分支資訊:
[root@centos7-2 jack-git]# git ls-remote origin
2742321ca81a99c464d107ebbb405f76264d462e HEAD
1f6aec8e78685142779c8862940a2e55fb516eca refs/heads/jack
2742321ca81a99c464d107ebbb405f76264d462e refs/heads/master
be35aae7e71a8149dcace5f6a151ccd1f1249565 refs/heads/test
推送本地分支到遠端:
[root@centos7-2 jack-git]# git add test1
[root@centos7-2 jack-git]# git commit -m 'ch test1'
[test d3e19a6] ch test1
1 file changed, 5 insertions(+)
[root@centos7-2 jack-git]# git push origin test
檢視别名配置檔案資訊
[root@centos7-2 jack-git]# git config --list |grep alias
本文轉自 jiekegz 51CTO部落格,原文連結:http://blog.51cto.com/jacksoner/2049875