Skip to content

Git使用指南

前置配置

配置.bashrc文件

如果用的终端是bash,可以通过配置.bashrc文件来实现一些常用命令的别名,以及一些环境变量的配置。

shell
alias 自定义命令=系统命令

Example:

shell
alias ll='ls -al'    

alias git-log='git log --all --graph --pretty=oneline --abbrev-commit'

引入命令:

shell
source ~/.bashrc  # ~表示用户home路径
解决中文乱码
  1. 设置quotepath为false:

    shell
    git config --global core.quotepath false
  2. 打开bash.bashrc文件:

    shell
    vim ${git_home}/etc/bash.bashrc

    注: ${git_home}表示git安装路径。 vim是一个文本编辑器, 可以使用其他编辑器打开

  3. 配置语言为中文:

    shell
    # 添加下面两行
    export LANG="zh_CN.UTF-8" 
    export LC_ALL="zh_CN.UTF-8"
配置ssh
shell
# 生成ssh密钥
ssh-keygen -t rsa

# 获取公钥内容
cat ~/.ssh/id_rsa.pub

# 验证github等的公钥是否配置成功
ssh -T [email protected]
其它常用配置
shell
# 配置用户名
git config --global user.name 用户名

# 配置邮箱
git config --global user.email 邮箱

# 查看用户名
git config --global user.name

# 查看邮箱
git config --global user.email

# 配置代理
git config --global http.proxy http代理地址
git config --global https.proxy https代理地址

常用命令

初始化仓库
shell
git init
添加修改
shell
git add 文件   # .gitignore包含的内容不会被添加
提交修改
shell
git commit -m "提交信息"
查看提交状态
shell
git status
查看提交日志
shell
git log
	--all  # 显示所有分支
	--pretty=oneline  # 将提交信息显示为一行
	--abbrev-commit  # 使得输出的commitID更为简短
	--graph  # 以图的形式显示
版本回退
shell
git reset --hard commitID  # commitID可以使用git log指令查看
撤销一次提交
shell
git reset HEAD^
查看已经删除的记录
shell
git reflog
查看本地分支
shell
git branch
创建分支
shell
git branch 分支名
查看当前分支与远程分支的对应关系
shell
git branch -vv
切换分支
shell
git checkout 分支名
创建并切换分支
shell
git checkout -b 分支名
合并分支
shell
git merge 选中分支名 # 将选择分支合并到当前分支下
删除分支
shell
git branch -d 分支名 # 有检查

git branch -D 分支名 # 强制删除,不检查
查看远程仓库
shell
git remote
配置远程仓库名称
shell
git remote add [name] [远程仓库ssh]
推送到远程仓库
shell
git push [-f] [--set-upstream] [远端名称] [本地分支名] [:远端分支名]
已关联状态推送
shell
git push
克隆仓库
shell
git clone [远程仓库ssh] [指定地址]
抓取请求(不会自动合并)
shell
git fetch [远程仓库ssh] [分支名称]
拉取请求

抓取并自动合并,相当于fetch+merge。

shell
git pull [远端仓库ssh] [分支名称]
已关联的仓库拉取
shell
git pull

图形化工具操作

以下内容以idea界面为演示

添加个人账户
  1. 打开设置

  2. 添加账户

  3. 添加完成后如图示

创建git仓库
  1. 打开VCS

  2. 选择要作为git仓库的文件夹

  3. 创建完成后如下图所示

添加管理远程
  1. 找到管理远程

  2. 添加管理远程

  3. 添加url

    注: 请到以下位置寻找url

  4. 点击确定后没有弹出红色框框就算成功

    失败示例

拉取代码

创建PR

  1. 添加代码至暂存区并提交

  2. 推送代码

    注: 推送成功后为下图

  3. 在GitHub创建Pull Request