git常用命令

git常用命令

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
26
27
28
29
30
git clone https://github.com/username/project.github.io.git #拉取代码(master/main)
git clone -b _dev https://github.com/username/project.github.io.git #拉取分支(非master或main分支)
git checkout --track origin/_remote #获取指定的远程分支到本地

git branch #查看本地分支
git branch -a #查看远程分支
git branch -vv#查看分支的绑定信息

git branch _local #创建本地分支
git branch -d _local #删除本地分支(当前分支不能停留在要删除的分支上)
git checkout _local #切换到本地分支
git checkout -b _local # 创建并切换到本地创建的分支

git push --set-upstream origin _remote #创建远程分支
git branch -r -d origin/_remote #删除远程分支(记得push一下 git push origin _remote)
git checkout -b _local origin/_remote #创建本地分支绑定远程分支

git status #查看修改过代码的类
git diff #查看修改的代码

git add src/main/java/com/so/alg/RecommendServiceImpl.java #添加修改的代码
git commit -m "recommend feed modified" #给修改的代码添加注释
git pull origin _remote #从远程更新变动的代码
git push origin _local:_remote #提交

git merge _remote #合并分支到master上(需要先切换到master分支上,在执行合并)

git tag -a 2020071801 -m 'v2.0部署' #添加tag
git show 2020071801 #展示tag
git push --tags #提交tag

git配置SSH公钥和私钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1.生成公钥和私钥(邮箱为github注册邮箱)
ssh-keygen -t rsa -C "xxxxxx@gmail.com"
# 2.设置公钥私钥key的保存位置(可以直接确认则保存在默认位置)
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa):
# 3.输入、重复输入密钥盐值
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
# 4.复制打印的公钥内容,并在github->Settings->SSH and GPG keys->New SSH Key中设置(title随意起)
cat .ssh\id_rsa.pub
# 5.查看密钥是否配置成功(会提示输入盐值)
ssh -T git@github.com
Enter passphrase for key '/Users/username/.ssh/id_rsa':
Hi hicode360! You've successfully authenticated, but GitHub does not provide shell access.
# 6.配置全局git信息
git config --global user.name "username" # github用户名
git config --global user.email "xxxxxx@gmail.com" #github注册邮箱