常用操作

创建git仓库
1
2
3
4
5
6
7
8
mkdir test
cd test
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/xxx/test.git
git push -u origin "master"
查看仓库地址
1
git remote -v
更改远程仓库地址
1
git remote set-url origin <newUrl>
获取最新分支
1
2
#获取所有最新分支
git fetch
查看分支
1
2
3
4
5
6
7
8
#查看本地当前所在分支
git branch

#查看远程分支
git branch -r

#列出本地分支和远程分支
git branch -a
合并分支
1
2
3
4
5
6
7
8
9
10
11
12
13
#合并整个分支,<branch>为需要合并到当前分支的目标分支名称
git merge <branch>
#放弃此次合并
git merge --abort

#合并某个commit到当前分支(只包含此commit)
git cherry-pick <commitId>
#合并某个范围的commit到当前分支(带^表示包含第一个分支)
git cherry-pick <commitId>^..<commitId>
#继续合并,合并后发生处突会导致合并中断,冲突处理完成后,输入这个指令可以继续合并
git cherry-pick --continue
#放弃合并
git cherry-pick --abort
创建分支
1
2
#创建分支
git branch <branchName>
删除历史commit

删除最后一次提交

1
2
3
4
5
#第一步:回滚上一次提交
git reset --hard HEAD^

#第二步:强制提交本地代码(如果没办法强制提交,去github或gitee上设置一下,有可能是分支被保护了,没办法强制提交)
git push origin <分支名称> -f

删除某次提交

1
2
3
4
5
6
7
8
9
10
##第一步:提交回滚
#回滚到某次提交
git reset <commitId>
#此次提交之后的修改会被退回到暂存区
git reset --soft <commitId>
#此次提交之后的修改不做任何保留
git reset --hard <commitId>

##第二步:强制提交本地代码(如果没办法强制提交,去github或gitee上设置一下,有可能是分支被保护了,没办法强制提交)
git push origin <分支名称> -f
标签

本地创建标签

1
2
3
4
5
#轻量级标签,指向一个特定commit的指针
git tag <tagName> <commitId>

#带注释的标签,会储存很多信息,包括注释、创建人、创建时间等
git tag -a <tagName> -m 'version 1.0' <commitId>

删除本地标签

1
git tag -d <tagName>

本地标签推送到远程

1
2
3
4
5
#推送本地所有标签
git push --tags

#推送本地某个标签
git push origin <tagName>

删除远程标签

1
git push origin :refs/tags/<tagName>

查看标签

1
2
3
4
5
6
#查看某个标签的详细信息
git show <tagName>

#查看所有标签
git tag
git tag -l

切换到指定标签

1
2
3
4
5
#切换到指定标签,但是无法编辑代码
git checkout <tagName>

#从指定标签创建一个新分支,可以编辑
git checkout -a <newBranch> <tagName>

问题

  1. SSL: no alternative certificate subject name matches target host name

    fatal: unable to access ‘https://ip_or_domain/xx/xx.git/': SSL: no alternative certificate subject name matches target host name ‘ip_or_domain’

    在克隆或推送代码的时候会出现这个这个问题,该错误为:没有与目标主机名匹配的证书。如果没有办法修复证书问题,可以选择git跳过ssl证书的校验,这样就可以正常克隆或推送代码。

    1
    git config --global http.sslVerify false