Git常用命令
1、设置用户签名
- 签名的作用是区分不同操作者身份。用户的签名信息在每个版本的提交信息中能够看到,确认此次提交是谁做的。Git首次安装必须设置一下用户签名,否则无法提交代码(与将来登录GitHub的账号没有任何关系)
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/桌面
$ git config --global user.name Juice
# 用户名
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/桌面
$ git config --global user.email Juice@atguigu.com
# 可以设置虚拟邮箱,不会验证
- 查看用户签名是否设计好了
2、初始化本地库
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo
$ git init
Initialized empty Git repository in D:/Git-Space/git-demo/.git/
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ ll
total 0
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ ll -a
total 4
drwxr-xr-x 1 Guozhiwei 197121 0 Jul 27 14:14 ./
drwxr-xr-x 1 Guozhiwei 197121 0 Jul 27 14:11 ../
drwxr-xr-x 1 Guozhiwei 197121 0 Jul 27 14:14 .git/
- 初始化好的Git Bash里面用的是linux命令
3、查看本地库状态
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
- 首次查看本地库状态会出现的提示:
- 写入hello.txt后,再次查看本地库状态
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ 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)
4、添加暂存区
- 将工作区的文件添加到暂存区
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
-
此时用git status查看状态发现变成绿色(当前git已经追踪到了这个文件,存在于暂存区里面)
-
如果此时不想保留(暂存区中的文件可以删掉)可以按照指令删除(但仍然在工作区中)
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git rm --cached hello.txt
rm 'hello.txt'
5、提交本地库
- 提交到本地库形成历史版本
- 语法:git commit -m “日志信息” 文件名 #日志信息是版本的信息
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git commit -m "first commmit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) 29803fb] first commmit
1 file changed, 13 insertions(+)
create mode 100644 hello.txt
- 可以用指令来查看日志信息(git log 详细 git reflog(Reference logs(参考日志)))
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git reflog
29803fb (HEAD -> master) HEAD@{0}: commit (initial): first commmit
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git log
commit 29803fb3d80b8f088d3efcf0ae36e36c77ff220d (HEAD -> master)
Author: Juice <Juice@atguigu.com>
Date: Wed Jul 27 19:08:56 2022 +0800
first commmit
6、修改文件
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ vim hello.txt
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
- 修改hello.txt后进行git status查看状态 发现文件名称为红色(为上传至暂存区)
- 第二次提交后进行本地库状态检查
- git无法识别修改,因此只能将修改的那一行先删除再加进去(表现为加了一行减了一行)
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git add hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.txt
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git commit -m "second commit" hello.txt
warning: in the working copy of 'hello.txt', LF will be replaced by CRLF the next time Git touches it
[master 0271db0] second commit
1 file changed, 1 insertion(+), 1 deletion(-)
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git status
On branch master
nothing to commit, working tree clean
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git reflog
0271db0 (HEAD -> master) HEAD@{0}: commit: second commit
29803fb HEAD@{1}: commit (initial): first commmit
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git log
commit 0271db050bfa854cd72ffa3278a792b3c8a8bf3d (HEAD -> master)
Author: Juice <Juice@atguigu.com>
Date: Wed Jul 27 22:50:37 2022 +0800
second commit
commit 29803fb3d80b8f088d3efcf0ae36e36c77ff220d
Author: Juice <Juice@atguigu.com>
Date: Wed Jul 27 19:08:56 2022 +0800
first commmit
注意事项:工作区的版本为头指针指向的版本
7、版本穿梭
- 先用git reflog查看版本号
- 语法:git reset --hard 版本号
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git reset --hard 0271db0
HEAD is now at 0271db0 second commit
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git reflog
0271db0 (HEAD -> master) HEAD@{0}: reset: moving to 0271db0
0bae5fa HEAD@{1}: commit: third commit
0271db0 (HEAD -> master) HEAD@{2}: commit: second commit
29803fb HEAD@{3}: commit (initial): first commmit
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ cat hello.txt
hello atguigu! hello git! 22222222222222
- git切换版本,底层其实是移动HEAD指针
8、Git分支操作
- 在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。(简单理解:分支为副本,一个分支就是一个单独的副本,底层也是指针的引用)
- 示例
- 好处:
- 同时并行推进多个功能开发,提高开发效率
- 各个分支再开发过程中,如果一个分支开发失败,不会对其他分支有任何影响,失败分支删除重现开始即可。
1、查看分支
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git branch -v
* master 29803fb first commmit
2、创建分支
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git branch hot-fix
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git branch -v
hot-fix 29803fb first commmit
* master 29803fb first commmit
- 后面括号是master说明目前仍然在master分支上
3、切换分支
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (hot-fix)
$ git branch -v
* hot-fix 29803fb first commmit
master 29803fb first commmit
- 切换分支的本质就是移动HEAD指针
4、修改分支
- 在master分支上做修改
5、合并分支
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git merge hot-fix
Updating 29803fb..c268b06
Fast-forward
hello.txt | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
6、产生冲突
-
合并分支时,两个分支在同一个文件的同一个位置有两套完全不一样的修改。Git无法替我们决定使用哪一个。必须人为决定新代码内容。
-
操作:修改master分支(master test)和hot-fix分支(hot-fix test)后进行合并
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master|MERGING)
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
- 进入hello.txt后查看
- <和=之间是当前分支 >和=之间是要合并的分支
- 如果两行都想要,则将多余的删掉
- 用linux命令 dd
- 修改完以后查看本地库状态,然后再提交(git add)
- 注意:git commit不能写文件名,否则报错:cannot do a partial commit during a merge
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master|MERGING)
$ git commit -m "merge test"
[master 7eb8d8e] merge test
- 在master上进行合并(修改)hot-fix是不会收到影响的
9、Git团队协作机制
-
团队内协作
-
跨团队协作
10、GitHub操作
1、创建远程库别名
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (hot-fix)
$ git remote -v
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (hot-fix)
$ git remote add git-demo https://github.com/GZW18620285024/git-demo.git
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (hot-fix)
$ git remote -v
git-demo https://github.com/GZW18620285024/git-demo.git (fetch)
git-demo https://github.com/GZW18620285024/git-demo.git (push)
2、推送本地分支到远程仓库
- 最小单位是分支,选择推送本地库的一个分支
- 选择token登录(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)
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git push git-demo master
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 12 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (21/21), 1.53 KiB | 523.00 KiB/s, done.
Total 21 (delta 7), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (7/7), done.
To https://github.com/GZW18620285024/git-demo.git
* [new branch] master -> master
3、拉取远程库到本地库
- 要保证本地库的代码是最新的
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-demo (master)
$ git pull git-demo master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 668 bytes | 133.00 KiB/s, done.
From https://github.com/GZW18620285024/git-demo
* branch master -> FETCH_HEAD
7eb8d8e..e1004b4 master -> git-demo/master
Updating 7eb8d8e..e1004b4
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
4、克隆远程仓库到本地
- 克隆代码不需要登陆账号(public)
- 克隆做的三件事:
- 拉取代码
- 初始化本地仓库
- 创建别名
Guozhiwei@LAPTOP-J3RSC0CV MINGW64 /d/Git-Space/git-democopy
$ git clone https://github.com/GZW18620285024/git-demo.git
Cloning into 'git-demo'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 24 (delta 8), reused 20 (delta 7), pack-reused 0
Receiving objects: 100% (24/24), done.
Resolving deltas: 100% (8/8), done.
5、团队内协作
- 必须要先邀请成为成员(一个团队),才可以进行push代码