Git基本使用
一、git的基本应用
查看帮助命令
git --help
1、git是分布式版本控制系统,不同的人提交的代码需要区分,所有需要设置身份验证
git config --global user.name "Zhangsan" # <用户名>
git config --global user.email "8819343@qq.com" # <邮箱>
2、创建本地仓库
在windows中新建一个目录,然后在目录中初始化本地仓库
git init //初始化
ls .git //可以查看到有这个文件,windowsc查看不到是因为隐藏了,具体方法自行查找
3、添加文件到缓存区(也叫暂存区)
随便新建一个文件,我这里新建的是hello.txt,用于测试文件,并在hello.txt中新建内容
使用git add提交到暂存区
git add hello.txt
4、查看目录有哪些文件需要提交
$ 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")
5、使用git commit提交,-m显示提交信息
$ git commit -m "修改hello文件"
[master 2ec07c4] 修改hello文件
1 file changed, 1 insertion(+), 10 deletions(-)
再次查看文件状态
$ git status
On branch master
nothing to commit, working tree clean
小结:
- 工作目录中写好的代码文件需要先
git add 文件名
添加到暂存区,再git commit 文件名
提交。以后每次修改都要重复前两步。 git status
查看工作目录中的状态git diff 文件名
查看文件修改了什么
6、使用git log查看提交的历史版本信息
$ git log
commit 7c6f7f4151cded346249ee492491a68c6047e24d (HEAD -> master)
Author: Gandolf <m15572018661@163.com>
Date: Mon Apr 17 16:34:40 2023 +0800
添加一行test Linux
commit f8e242e7adc2eefb2b6de93099359d57e63d84c5
Author: Gandolf <m15572018661@163.com>
Date: Mon Apr 17 16:33:25 2023 +0800
添加了一行打印你好世界!
commit d5f5bc6f9fb4f738b1e1060261bd9244025ad5ab
Author: Gandolf <m15572018661@163.com>
Date: Mon Apr 17 16:31:37 2023 +0800
提交测试文件
使用这个命令查看的更加简洁
$ git log --pretty=oneline
7c6f7f4151cded346249ee492491a68c6047e24d (HEAD -> master) 添加一行test Linux
f8e242e7adc2eefb2b6de93099359d57e63d84c5 添加了一行打印你好世界!
d5f5bc6f9fb4f738b1e1060261bd9244025ad5ab 提交测试文件
7、如需回退,可以进行版本回退与还原
//查看版本号 git reflog
$ git reflog
91f45e9 (HEAD -> master) HEAD@{0}: commit: 增加hello world!
2ec07c4 HEAD@{1}: commit: 修改hello文件
45fe862 HEAD@{2}: commit: my second commit
131d5ea HEAD@{3}: commit (initial): my first commit
//退回版本号
$ git reset --hard 2ec07c4
HEAD is now at 2ec07c4 修改hello文件
//查看文件是否退回
$ cat hello.txt
Welcome to Middle Earth!
小结:
- 使用git reflog查看提交的版本号
- 使用git reset --hard <版本号>你可以自由的在不同版本号之间来回切换
注意:
- 我们在工作是任何修改或者文件的变动,都需要先提及到暂存区 git add <文件名>,让暂存区和工作目录的状态一致,才能提交这个版本。
- git commit是提交的暂存区的所有文件状态,也就是把工作目录的状态保存为一个版本,不是一个文件。
二、分支管理
每个项目都有一个主分支,而其它分支就是主分支的一个副本,在分支上写代码不会影响到主分支,比如说有两个小组开发两个功能模块,它们有各自的分支就互不影响,开发测试完毕后就可以合并到主分支上
1、查看分支
$ git branch
* master
2、创建分支
$ git branch test
$ git branch
* master
test
3、切换分支
$ git checkout test
Switched to branch 'test'
$ git branch
master
* test
4、合并分支
//在test分支中向hello.txt文件追加内容
$ echo "print('hello')" >> hello.txt
//查看test.txt文件
$ cat hello.txt
Welcome to Middle Earth!print('hello')
//查看文件状态
$ git status
On branch test
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")
//添加暂存区
$ 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 commit -m "合并分支"
[test 2958988] 合并分支
1 file changed, 1 insertion(+), 1 deletion(-)
//切换分支master
$ git checkout master
Switched to branch 'master'
//查看分支
$ git branch
* master
test
//查看文件,发现在test中追加的内容,在master中没有,是因为master和testb需要手动合并,本就是完全不相同的两个分支。
$ cat hello.txt
Welcome to Middle Earth!
//把test的分支合并到master中,此时的分支是master
$ git merge test
Updating 2ec07c4..2958988
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
//再次查看,发现有追加的内容,合并成功!
$ cat hello.txt
Welcome to Middle Earth!print('hello')
拉去代码用法
总结简单使用:
- git clone 你的https/ssh地址克隆远程仓库到本地,不克隆直接push会报错的
git add 文件名
添加文件到暂存区git commit -m "提交描述"
将文件提价到版本库git push
将文件push到远程仓库
如有错误还需指正!谢谢