Git 概述
git 分區(qū)原理
Git 常用指令
設(shè)置用戶簽名
初始化本地庫
查看本地庫狀態(tài)
git status
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use 'git add' to track)
$ git status
On branch master
No commits yet
Untracked files:
(use 'git add <file>...' to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use 'git add'
to track)
添加暫存區(qū)
git add 文件名
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working
directory.
$ git status
On branch master
No commits yet
Changes to be committed:
(use 'git rm --cached <file>...' to unstage)
new file: hello.txt
提交本地庫
git commit -m '日志信息' 文件名
$ git commit -m 'my first commit' hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working
directory.
[master (root-commit) 86366fa] my first commit
1 file changed, 16 insertions(+)
create mode 100644 hello.txt
$ git status
On branch master
nothing to commit, working tree clean
修改文件
$ 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: hello.txt
no changes added to commit (use 'git add' and/or 'git commit -a')
查看歷史版本
git reflog 查看版本信息
git log 查看版本詳細(xì)信息
$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit
版本
git reset --hard 版本號
$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit
--切換到 86366fa 版本,也就是我們第一次提交的版本
$ git reset --hard 86366fa
HEAD is now at 86366fa my first commit
--切換完畢之后再查看歷史記錄,當(dāng)前成功切換到了 86366fa 版本
$ git reflog
86366fa (HEAD -> master) HEAD@{0}: reset: moving to 86366fa
087a1a7 HEAD@{1}: commit: my third commit
ca8ded6 HEAD@{2}: commit: my second commit
86366fa (HEAD -> master) HEAD@{3}: commit (initial): my first commit
--然后查看文件 hello.txt,發(fā)現(xiàn)文件內(nèi)容已經(jīng)變化
Git 分支操作
分支基本操作
查看分支
git branch -v
$ git branch -v
* master 087a1a7 my third commit (*代表當(dāng)前所在的分區(qū))
創(chuàng)建分支
git branch 分支名
$ git branch hot-fix
$ git branch -v
hot-fix 087a1a7 my third commit (剛創(chuàng)建的新的分支,并將主分支 master
的內(nèi)容復(fù)制了一份)
* master 087a1a7 my third commit
分支開發(fā)
切換分支
git checkout 分支名
$ git checkout hot-fix
Switched to branch 'hot-fix'
--發(fā)現(xiàn)當(dāng)先分支已由 master 改為 hot-fix
合并分支
git merge 分支名
1、切換到主支
2、git merge 旁支名
合并沖突解決
hello git! hello atguigu!
<<<<<<< HEAD
hello git! hello atguigu! master test
hello git! hello atguigu!
=======
hello git! hello atguigu!
hello git! hello atguigu! hot-fix test
>>>>>>> hot-fix
1、刪掉沖突中不要的部分,留下最終的部分
2、git add 添加到暫存區(qū)
3、 git commit -m 注意,此時的 commit 后面不能帶文件名。帶文件名是幾個意思呢?到底用哪個呢?
然后就會發(fā)現(xiàn)合并成功了。
Git 團(tuán)隊協(xié)作機制
團(tuán)隊協(xié)作
跨團(tuán)隊協(xié)作
遠(yuǎn)程倉庫
別名
$ git remote -v
$ git remote add ori https://github.com/xxx.git
$ git remote -v
ori https://github.com/xxx.git (fetch)
ori https://github.com/xxx.git (push)
推送本地倉庫到遠(yuǎn)程倉庫
git push 別名 分支
$ git push ori master
Logon failed, use ctrl+c to cancel basic credential prompt.
Username for 'https://github.com': atguiguyueyue
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/atguiguyueyue/git-shTest.git
* [new branch] master -> master
克隆遠(yuǎn)程倉庫到本地
拉取遠(yuǎn)程庫內(nèi)容
git pull 遠(yuǎn)程庫地址別名 遠(yuǎn)程分支名
$ git pull ori master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/atguiguyueyue/git-shTest
* branch master -> FETCH_HEAD
7cb4d02..5dabe6b master -> ori/master
Updating 7cb4d02..5dabe6b
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
???????????????? END ???????????????