1、简介
Git 是一种分布式版本控制系统。它可以不受网络连接的限制,加上其它众多优点,目前已经成为程序开发人员做项目版本管理时的首选,同时非开发人员也可以用Git管理私人的文档。
Git 的命令有很多,但对于初学者和平时项目需求来说,只需记住几个最基本的命令即可。正所谓“贪多嚼不烂”,本文只适用于初学者和只是想简单了解使用的人群。
下面就是初学者需要掌握的Git命令列表,先大致看一下
git clone
git config
git branch
git checkout
git status
git add
git commit
git push
git pull
git log
git tag
接下来,将通过对:https://gitee.com/xiaocai-zhang/gitstudy.git(此地址的由来下面【3、生成远程版本仓库地址】会讲到)
仓库进行实例操作,讲解如何使用 Git
拉取代码到提交代码的整个流程。
2、安装Git
参考文章:Git安装
3、生成远程版本仓库地址
在这里我用的是Gitee(Git的代码托管平台),市面上有很多这种Git的代码托管平台,例如:Github等,大家可以根据自己的喜好、使用习惯选择。
点击地址:Gitee - 基于 Git 的代码托管和研发协作平台注册账号,并登录
点击【新建仓库】,填写仓库基本信息,点击创建
打开仓库,点击 【克隆/下载】,复制项目HTTPS地址 https://gitee.com/xiaocai-zhang/gitstudy.git,此地址就是我们的远程版本库地址
4、命令实例操作
(1)git clone
从远程版本库服务器拉取代码
在本地盘符中创建文件夹【gitstudy】,利用win+R,然后输入cmd,回车进入命令界面,使用命令“cd D:\gitstudy”跳转到刚才创建的【gitstudy】目录
代码:
cd D:\gitstudy
git clone https://gitee.com/xiaocai-zhang/gitstudy.git
运行结果:
本地文件夹
(2)git config
配置开发者用户名和邮箱
git config user.name 用户名
git config user.email 邮箱@qqqq.com
每次代码提交的时候都会生成一条提交记录,其中会包含当前配置的用户名和邮箱。
(3)git branch
创建、重命名、查看、删除项目分支,通过
Git
做项目开发时,一般都是在开发分支中进行,开发完成后合并分支到主干。
创建一个名为 develop/0.0.0
的日常开发分支,分支名只要不包括特殊字符即可。
git branch develop/0.0.0
如果觉得之前的分支名不合适,可以为新建的分支重命名,重命名分支名为 develop/0.0.1
git branch -m develop/0.0.0 develop/0.0.1
通过不带参数的branch命令可以查看当前项目分支列表,当前分支前面会标一个*
号
git branch
如果分支已经完成使命则可以通过 -d
参数将分支删除,这里为了继续下一步操作,暂不执行删除操作
git branch -d develop/0.0.1
(4)git checkout
切换分支
git checkout develop/0.0.1
切换到 develop/0.0.1
分支,后续的操作将在这个分支上进行
(5)git status
查看文件变动状态
通过任何你喜欢的编辑器对项目中的 README.md
文件做一些改动,保存。
git status
通过 git status
命令可以看到文件当前状态 Changes not staged for commit:
(改动文件未提交到暂存区)
(6)git commit
提交文件变动到本地版本库
git commit -m '这里写提交原因'
通过 -m
参数可直接在命令行里输入提交描述文本,
当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
方便自己也方便别人阅读。
(7)git push
将本地的代码改动推送到远程版本库服务器
git push origin develop/0.0.1
origin
指代的是当前的git服务器地址,这行命令的意思是把 develop/0.0.1
分支推送到服务器,当看到命令行返回如下字符表示推送成功了。
现在我们回到Gitee网站的项目首页,点击 master
下拉按钮,就会看到刚才推送的 develop/0.0.1
分支了
(7)git pull
将服务器上的最新代码拉取到本地
git pull origin develop/0.0.1
如果其它项目成员对项目做了改动并推送到服务器,我们需要将最新的改动更新到本地,这里我们来模拟一下这种情况。
进入Gitee网站的项目首页,再进入 develop/0.0.1
分支,在线对 README.md
文件做一些修改并保存,然后在命令中执行 git pull origin develop/0.0.1 命令,它将把刚才在线修改的部分拉取到本地,用编辑器打开 README.md
,你会发现文件已经跟线上的内容同步了。
注:如果线上代码做了变动,而你本地的代码也有变动,拉取的代码就有可能会跟你本地的改动冲突,一般情况下
Git
会自动处理这种冲突合并,但如果改动的是同一行,那就需要手动来合并代码,编辑文件,保存最新的改动,再通过git add .
和git commit -m 'xxx'
来提交合并。
(8)git log
查看版本提交记录
git log
通过以上命令,我们可以查看整个项目的版本提交记录,它里面包含了提交人
、日期
、提交原因
等信息,得到的结果如下:
提交记录可能会非常多,按 J
键往下翻,按 K
键往上翻,按 Q
键退出查看
(9)git tag
为项目标记里程碑
git tag publish/0.0.1 git push origin publish/0.0.1
当我们完成某个功能需求准备发布上线时,应该将此次完整的项目代码做个标记,并将这个标记好的版本发布到线上,这里我们以 publish/0.0.1
为标记名并发布,当看到命令行返回如下内容则表示发布成功了
Total 0 (delta 0), reused 0 (delta 0) To https://github.com/gafish/gafish.github.com.git * [new tag] publish/0.0.1 -> publish/0.0.1
(10).gitignore
设置哪些内容不需要推送到服务器,这是一个配置文件
touch .gitignore
.gitignore
不是 Git
命令,而在项目中的一个文件,通过设置 .gitignore
的内容告诉 Git
哪些文件应该被忽略不需要推送到服务器,通过以上命令可以创建一个 .gitignore
文件,并在编辑器中打开文件,每一行代表一个要忽略的文件或目录,如:
demo.html build/
以上内容的意思是 Git
将忽略 demo.html
文件 和 build/
目录,这些内容不会被推送到服务器上
小结
通过掌握以上这些基本命令就可以在项目中开始用起来了。
5、测试中出现的一些错误
(1)将当前版本库推送到develop/0.0.1
出现以下错误"src refspec debelop/0.0.1 does not match any"
PS D:\gitstudy\gitstudy> git push origin debelop/0.0.1
error: src refspec debelop/0.0.1 does not match any
error: failed to push some refs to 'https://gitee.com/xiaocai-zhang/gitstudy.git'
原因:
当前本地版本库没有在 develop/0.0.1
分支上,而我又往远程仓库的 develop/0.0.1
上 push,导致无法 push
排查问题,用 git branch 命令查看本地当前版本库,* 标识的就是当前版本库
PS D:\gitstudy\gitstudy> git branch
develop/0.0.1
* master
切换版本库
PS D:\gitstudy\gitstudy> git checkout develop/0.0.1
Switched to branch 'develop/0.0.1'
注意:文章里我是用的系统的命令行工具,GIT这里建议使用Git命令行工具,安装了GIT就会有,如下图:
(2)再次推送,此时再次推送失败,因为我本地分支的最新提交落后于远程分支的最新提交.
解决办法也很简单,Git已经提示我们,先用git pull
把最新的提交从origin/develop/0.0.1
抓下来,然后,在本地合并,解决冲突,再推送:
PS D:\gitstudy\gitstudy> git push origin develop/0.0.1
To https://gitee.com/xiaocai-zhang/gitstudy.git
! [rejected] develop/0.0.1 -> develop/0.0.1 (non-fast-forward)
error: failed to push some refs to 'https://gitee.com/xiaocai-zhang/gitstudy.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
(3)拉取远程分支,git pull
也失败了
根据提示是没有指定本地版本库develop/0.0.1
分支与远程版本库origin/develop/0.0.1
分支的链接
PS D:\gitstudy\gitstudy> git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> develop/0.0.1
那我们接下来就根据提示,设置develop/0.0.1
和origin/develop/0.0.1
的链接:
PS D:\gitstudy\gitstudy> git branch --set-upstream-to=origin/develop/0.0.1 develop/0.0.1
branch 'develop/0.0.1' set up to track 'origin/develop/0.0.1'.
再次拉取
git pull
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
(4)这回git pull
成功,但是合并有冲突,需要手动解决
Git用<<<<<<<
,=======
,>>>>>>>
标记出不同分支的内容,根据实际需求删除或保留代码即可,解决后,提交,再push:
修改后文件:
再执行一下命令提交代码到本地版本库
PS D:\gitstudy\gitstudy> git add .\README.md
PS D:\gitstudy\gitstudy> git commit -m "fix env conflict"
[develop/0.0.1 fa5014e] fix env conflict
最后再一次推送到远程版本库
PS D:\gitstudy\gitstudy> git push origin develop/0.0.1
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 356 bytes | 356.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 6747db88
To https://gitee.com/xiaocai-zhang/gitstudy.git
9c1a90d..fa5014e develop/0.0.1 -> develop/0.0.1
补充
(1)要查看远程库的信息,用git remote
:
PS D:\gitstudy\gitstudy> git remote
origin
或者,用git remote -v
显示更详细的信息:
PS D:\gitstudy\gitstudy> git remote -v
origin https://gitee.com/xiaocai-zhang/gitstudy.git (fetch)
origin https://gitee.com/xiaocai-zhang/gitstudy.git (push)
(2) git add
命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit
就可以一次性把暂存区的所有修改提交到本地的当前版本库。git push 才是将当前版本库所有本地提交推送到远程库。
PS D:\gitstudy\gitstudy> git add .\README.md
PS D:\gitstudy\gitstudy> git commit -m "修改注释"
[develop/0.0.1 1e99dd6] 修改注释
1 file changed, 1 insertion(+), 1 deletion(-)
PS D:\gitstudy\gitstudy> git push origin develop/0.0.1
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 379 bytes | 379.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 344381b5
To https://gitee.com/xiaocai-zhang/gitstudy.git
fa5014e..1e99dd6 develop/0.0.1 -> develop/0.0.1