git命令

12/26/2021 git

# 安装完git后必做

git config --global user.name "apophis"
git config --global user.email xxxxxxx@163.com
# 避免每次都要输入密码
git config --global credential.helper store
# 取消代理
git config --global --unset http.proxy 
git config --global --unset https.proxy
1
2
3
4
5
6
7

# 切换分支

git checkout dev
1

# 查看分支

git branch
1

# 删除本地分支

git branch -D dev
1

# 创建并切换分支

git checkout -b dev
1

# 合并分支

# 将dev的代码合并到当前分支(当前为master分支)
git merge dev
1
2

# 拉取

git pull origin master
1

# clone某个分支

git clone -b 远程分支名称  https://GitHub.com/username/xxx.git
1

# clone某个tag

git clone -b tag名称  https://GitHub.com/username/xxx.git
1

# push流程

# 1.进入对应的分支目录,将新建文件及修改文件提交至暂存区
git add .
# 2.提交到git本地(相当于确认提交),并添加注释
git commit -m "备注"
# 3.将本地修改同步到远程仓库的分支,如果该分支被其他人提交,则需要在pull之后重新执行步骤3
git push origin master
1
2
3
4
5
6

# 回滚流程

git rest命令 (opens new window)

# 仅提交到本地,未提交远程

# 回退到上个版本,保留代码
git reset --soft HEAD^
# 回退到上上个版本,保留代码
git reset --soft HEAD~2
1
2
3
4

# 已提交到远程

# 1.查找push的记录 git log -n(查看最近n条日志)
git log -3
# 2.回退到指定版本,不保留原更改代码
git reset --hard 记录id
# 2.回退到指定版本,保留原更改代码,且生成新的提交
git revert 记录id  
 
1
2
3
4
5
6
7

# 同时配置多个git仓库

修改remote即可,一次push到多个地址

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
[remote "gitee"]
	url = https://gitee.com/user/project.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[remote "github"]
	url = https://github.com/user/project.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Last Updated: 6/1/2024, 6:36:28 AM