开始
在开始使用GIT之前,首先要安装GIT客户端,根据自己的操作系统选择对应的版本并安装即可。
安装成功后,一般会在右键菜单项里面增加:
- Git Bash Here
- Git GUI Here
以下的所有教程都在Git Bash里面进行。
一般在开始使用GIT之前,我们需要在GIT里面配置下我们的名字和邮箱:
$ git config --global user.name "Your Name Comes Here"
$ git config --global user.email you@yourdomain.example.com
常用的GIT仓库:
接下来开始创建工作目录
# 初始化一个GIT工作目录
$ git init <project_name>
# 克隆一个现有的工作目录(这个比较常见)
# Clone with ssh
$ git clone git@github.com:<id>/<project_name>.git
# Clone with Https
$ git clone https://github.com/<id>/<project_name>.git
使用SSH通讯需要生成公私钥并添加到自己账户下,详见github的教程。
创建或克隆号工作目录之后,我们需要先进入到工作目录
$ cd <project_name>
日常操作
暂存本地修改
# 添加指定文件
$ git add <file_name>
# 强制添加指定文件(常用于被gitignore忽略的文件)
$ git add -f <file_name>
# 按指定规则添加文件
$ git add git-*.sh
# 添加所有文件
$ git add -A
$ git add -all
撤销本地暂存
$ git restore --staged <file>
重置本地修改
$ git restore <file>
查看状态
$ git status
提交修改
$ git commit -m "Your commit message"
推送修改到远程仓库
# 推送当前分支修改到远程仓库origin/master分支
$ git push origin master
# 建立当前分支和远程分支master的关联
$ git push -u origin master
# 建立关联后可以使用简化的命令(不推荐)
$ git push
更新本地仓库
# 拉去远程仓库origin/master分支,并和本地分支合并
$ git pull origin master
# 等价于下面两步操作
$ git fetch origin master
$ git merge origin/master
# 也可以配置链接简化命令
$ git branch --set-upstream-to <branch-name> origin/<branch-name>
# 接下来可以直接使用
$ git pull
远程仓库相关操作
# 查看远程仓库
$ git remote -v
# 移除远程仓库
$ git remote remove <remote_name>
# 添加远程仓库
$ git remote add <remote_name>
# 使用remote实现clone操作
$ mkdir <project_name>
$ cd <project_name>
$ git init
$ git remote origin https://github.com/<id>/<project_name>.git
$ git pull origin master
分支相关操作
# 查看本地分支
$ git branch
# 查看所有分支
$ git branch -a
# 创建分支
$ git branch <branch>
# 切换分支
$ git switch <branch>
# 创建并切换分支
$ git switch -c <branch> --track <remote>/<branch>
# 或使用旧的checkout命令
$ git checkout <branch>
$ git checkout -b <branch> --track <remote>/<branch>
# 删除分支
$ git branch -d <branch>
查看日志
$ git log
查看修改
# 查看未暂存修改和本地工作缓存的差异
$ git diff
# 使用工具会比较方便,类似(Beyond Compare)
# 配置工具
$ git config --global diff.tool
$ git config --global difftool.Beyond.cmd "'C:\Program Files\Beyond Compare 4\BCompare.exe' \"\$LOCAL\" \"\$REMOTE\""
$ git config --global difftool.prompt false
# 使用工具查看差异
$ git difftool
贮藏
# 贮藏本地所有修改
$ git stash
# 把贮藏的修改重新应用
$ git stash pop
# 查看本地贮藏记录
$ git stash list
# 应用贮藏,不删除
$ git stash apply
# 常见用法
$ git stash
$ git pull origin master
$ git stash pop
$ git push origin master
网友评论