什么是Git?
安装Git
Git常用指令
一、什么是Git
版本控制系统
- 可以用 Git 把代码提交到本地,可以看到代码的提交和更改记录,如果代码有 bug 还能进行代码还原
- 可以用 Git 把代码提交到远程服务器中,防止代码丢失
- 如果是团体开发,可以远程同步同一份代码,互不影响
二、安装Git
右键 Git Bash Here 使用 Git 自带的命令行终端

也可以配置系统环境变量,使用windows的命令行终端


三、Git具体指令
在终端中输入 git --help 查看一下常用的git指令

开始一个工作区间
git init
新建一个文件夹Test,切换到Test目录后,输入 git init 初始化一个空的 Git 仓库

Test文件夹下会生成 .git 的文件夹,这个就是本地的 Git 仓库了

git clone
切换到E盘,输入git clone D:\Test 将刚才的 Test 文件夹中的 Git 仓库拷贝过来,当然文件目录Test还是在的,但是不会拷贝除了仓库外的其它文件

这里会提示拷贝了一个空的仓库,因为之前只初始化什么操作都还没做,一般是用来拷贝远程服务器的仓库,只要把本地路径替换成远程仓库地址就可以了

研究当前的变化
git add
新建一个test.txt的文件,输入 git add D:\Test\test.txt , 会被添加到index索引,然后提示有新文件可以被commit提交了,而且可以使用 git rm --cached 移除索引中的文件。如果用 Git 自带的命令行终端路径用相对路径就可以了

git rm
输入 git rm --cached test.txt 移除索引中的test.txt文件

输入 git rm ,会提示用法

查看历史记录和状态
git status
这个命令可以查看当前仓库的状态,可以经常使用来熟悉 Git 指令
如还没初始化时,输入 git init ,会提示当前目录不是一个 Git 仓库

如已经初始化了,输入 git init ,会提示还没有文件被跟踪,如新建的text.txt文件,使用 git add 添加并提交文件

git log
输入 git log 可以查看提交记录。Git 自带的命令行终端可以看到作者、日期和提交信息


迭代、标记、调整
git commit
输入git commit -m "first commit" 提交,-m后跟的提示信息表示第一次提交

提交信息是必须的,只输入 git commit 会出现如下提示,并且提交会被终止。输入:q!回到原来目录

git branch
branch表示分支,在用 git init 初始化 Git 仓库时,会默认创建一个 master 的分支

输入 git branch a 新建分支,只要在git branch 后添加分支名称即可。输入 git branch 查看所有分支,发现有 a 和 master 两个分支,*代表当前使用的分支

输入 git branch -d a 删除分支a,git branch -D表示强制删除

git checkout
输入 git checkout a 切换分支,会从分支 master 切换到分支 a

输入 git checkout -b a 表示创建 a 分支,并切换到 a 分支
git merge
输入 git merge a 合并分支,这时要在不同的分支下

git tag
输入 git tag v1.0.0 给仓库加个标签,git tag 后跟标签。输入 git tag 查看已添加的标签

合作
git push
用来提交代码到远程仓库
输入 git remote add origin https://github.com/wmjwmj828/UtilBase.git,表示关联远程仓库,orgin表示远程仓库名称,后面跟远程仓库地址
输入 git push origin master 向远程仓库提交代码
git pull
拉取远程仓库中的代码
网友评论