从其它平台迁移而来


安装

Windows

  1. 下载git,由于某些原因,建议去镜像站点下载对应的版本(建议下载便携版)。

  2. 便携版进行自解压,选择合适路径,建议路径不要有中文。

  3. 环境变量中修改Path的值,增加git路径\bin

若有需要,也可以安装TortoiseGit图形操作界面。

其实Win10+启用wsl,然后在wsl里安装使用git是最爽的。

Linux

1
sudo apt update && sudo apt install git

基本配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 用户名和邮箱
git config --global user.name "XXX"
git config --global user.email XXXXXX@XXX.com
# 换行符
git config --global core.safecrlf true
git config --global core.autocrlf input # Windows 平台设为 true
# 记住密码
git config --global credential.helper store
# 别名设置
git config --global alias.lga "log --color --graph --pretty=format:'%C(auto)%h%C(auto)%d%Creset %s %C(bold blue)<%an> %C(yellow)%cd%Creset' --date=format:'%Y-%m-%d %H:%M' --abbrev-commit --all"
git config --global alias.lg "log --color --graph --pretty=format:'%C(auto)%h%C(auto)%d%Creset %s %C(bold blue)<%an> %C(yellow)%cd%Creset' --date=format:'%Y-%m-%d %H:%M' --abbrev-commit"

本地操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 初始化仓库
git init
# 查看状态
git status
# 添加修改到暂存区
git add . # 添加当前目前下所有文件修改
git add xxx # 添加指定文件
# 提交到仓库
git commit # 进入交互界面填写提交信息后提交
git commit -m '提交信息' # 直接填写提交信息并提交
# 创建分支
git branch xxx
# 创建并切换到新分支
git checkout -b xxx
# 检出分支
git checkout xxx
# 切换分支
git switch xxx
# 合并分支
git merge xxx
# 删除分支
git branch -d xxx # 删除前检查 merge 状态
git branch -D xxx # 直接强制删除
# 查看提交日志
git log

远程操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 克隆仓库
git clone 远程仓库地址 本地路径
# 查看远程仓库信息
git remote -v
# 添加远程仓库地址
git remote add 仓库别名 仓库地址
# 修改远程仓库别名
git remote rename 旧名称 新名称
# 移除远程仓库
git remote remove 仓库别名
# 获取远程仓库提交信息
git fetch # 加 --all 表示遍历全部远程仓库
# 拉取远程提交的代码
git pull # 默认与本地代码进行 merge
# 推送本地提交到远程仓库
git push
# 删除远程分支
git push 仓库别名 :分支名 # 推送空分支即删除