Git常用命令

日常使用

git

1. 安装

参考链接 关闭SSL认证 找到.gitconfig文件

1
2
3
4
[http]
sslverify = false
[url “https://”]
insteadOf = git://

2. 配置

1
2
3
4
5
6
名称
`git config --global user.namer "username"`
邮箱
`git config --global user.email "email"`
自动颜色
`git config --global color.ui auto`

可以在.gitconfig文件中查看

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[user]
    namer = 
    email = 
[color]
    ui = auto
[http]
sslverify = false
[url "https://"]
insteadOf = git://
[init]
    defaultBranch = main

3. SSH-key

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
设置私钥密码
`ssh-keygen -t rsa -C "email`
>第一个输入回车,接着输入fingerprint密码

回到github页面设置SSH
![github ssh.jpg](../_resources/github%20ssh.jpg)
key中填入返回的结果
`cat ~/.ssh/id_rsa.pub`

然后输入即可生效
`ssh -T git@github.com`

4. 仓库一般操作

 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
克隆仓库
`git clone git@github.com:cancanneed64/test.git`

github需先创建仓库
本地创建仓库并初始化
`mkdir test `
`git init`

查看状态
`git status`

暂存区添加
`git add`

添加注释并提交
`git commit -m ""`
> 如果直接git commit会打开编辑器,如果直接关闭会中止

查看日志
`git log`

简述多个
`git log --pretty=short`

前后差别
`git log -p`

查看工作树和暂存区的差别
`git diff`

最新一次提交
`git diff head`

## 5. 分支操作

显示分支
`git branch`

切换分支(-代替上一个分支)
`git checkout master`

创建切换分支
`git checkout -b feature-A`

合并分支
`git merge`

记录合并
`git merge --no-ff feature-A`

图表形式查看分支
`git log --graph`

恢复分支节点状态
`git reset --hard `

查看当前仓库执行过的操作日志,git log只能看以当前状态为终点的日志
`git reflog`

解决冲突示范

1
2
3
4
5
6
git checkout master
git reset --hard
git merge --no-ff fix-B
修改内容
git add 
git commit -m ""

修改上一条信息 git commit --amend

压缩历史,合并上一个提交 git rebase -i

HEAD-2选定当前分支中包含HEAD在内的两个最新历史记录为对象在编辑器中打开

6.仓库推拉

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
添加远程仓库,设置为origin
`git remote add origin git@github.com:id/file.git`

本地仓库内容推送到远程仓库
`git push -u origin master`

获取远程仓库,自动设置为origin,本地与仓库master分支相同
`git clone git@github.com:id/file.git`

查看远程和本地的分支
`git branch -a`

以名为origin 的仓库(这里指GitHub 端的仓库)的feature-D 分
支为来源,在本地仓库中创建feature-D 分支
`git checkout -b feature-D origin/feature-D`

从远程仓库拉取
`git pull origin feature-D`

7. Pull request

1
2
3
4
5
给原仓库设置名称
`git remote add upstream git://github.com/file.git`

从远程仓库获取最新代码,将upstream/master 分支与当前分(master)合并
`git fetch upstream`

error git push origin master remote: error: GH007: Your push would publish a private email address.

  • Keep my email addresses private

8. 一般操作

…or create a new repository on the command line

1
2
3
4
5
6
7
echo "# sample" >> README.md
  git init
  git add README.md
  git commit -m "first commit"
  git branch -M main
  git remote add origin https://github.com/melodyka/sample.git
  git push -u origin main

…or push an existing repository from the command line

1
2
3
git remote add origin https://github.com/melodyka/sample.git
  git branch -M main
  git push -u origin main

…or import code from another repository

1
You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

! [rejected] main -> main (fetch first) error: failed to push some refs to

9. 其他操作

撤回未推送最新一次提交 git reset HEAD^ 或 直接删除git重新开始

切换仓库 git remote set-url origin URLgit remote rm origin git remote add origin url

删除仓库内容

1
2
3
4
git rm * -f -r#删除所有文件夹包括文件
git add .
git commit -m "***" #增加提交信息
git push origin master#master是远程分支
最后更新于 10月04日 00点15分, 2024年