Git 概述
git 分区原理
Git 常用指令
设置用户签名
初始化本地库
查看本地库状态
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)
添加暂存区
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 查看版本详细信息
$ 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
--切换完毕之后再查看历史记录,当前成功切换到了 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,发现文件内容已经变化
Git 分支操作
分支基本操作
查看分支
git branch -v
$ git branch -v
* master 087a1a7 my third commit (*代表当前所在的分区)
创建分支
git branch 分支名
$ git branch hot-fix
$ git branch -v
hot-fix 087a1a7 my third commit (刚创建的新的分支,并将主分支 master
的内容复制了一份)
* master 087a1a7 my third commit
分支开发
切换分支
git checkout 分支名
$ git checkout hot-fix
Switched to branch 'hot-fix'
--发现当先分支已由 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 添加到暂存区
3、 git commit -m 注意,此时的 commit 后面不能带文件名。带文件名是几个意思呢?到底用哪个呢?
然后就会发现合并成功了。
Git 团队协作机制
团队协作
跨团队协作
远程仓库
别名
$ 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)
推送本地仓库到远程仓库
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
克隆远程仓库到本地
拉取远程库内容
git pull 远程库地址别名 远程分支名
$ 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 ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧
联系客服