Post

Git Base

Git default

Git 全局设置:

  • git config –global user.name “Cc”
  • git config –global user.email “li_chao_cheng@163.com”

创建 git 仓库:

  • mkdir client_user_server
  • cd client_user_server
  • git init
  • touch README.md
  • git add README.md
  • git commit -m “first commit”
  • git remote add origin https://gitee.com/Cc/Cc.git
  • git push -u origin master

已有仓库?

  • cd existing_git_repo
  • git remote add origin https://gitee.com/Cc/Cc.git
  • git push -u origin master

SSH 管理本地Code

  • 通过命令 ssh-keygen 生成 SSH Key
    • t key 类型
    • C 注释
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ssh-keygen -t rsa -C "li_chao_cheng@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/admin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/admin/.ssh/id_rsa.
Your public key has been saved in /c/Users/admin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:qbiJsDmN4w1ZiJuH1nWNkMeXdaYrsFnYJ91dFAjDr0k li_chao_cheng@163.com
The key's randomart image is:
+---[RSA 2048]----+
|          .+.o.o+|
|      o o +.*.. .|
|     o = * +.. . |
|. .   o O.oE..   |
|.. . . =So..o    |
| ++ ....  .o     |
|=*... .          |
|==+. o           |
|++..o            |
+----[SHA256]-----+

Cc@DESKTOP-3Q5O26A MINGW64 /d/go/gopath/src/lichaocheng/yun_mao (master)
  • 查看生成的 SSH 公钥和私钥:
    • 私钥文件 id_rsa 公钥文件 id_rsa.pub
1
ls ~/.ssh/
  • 查看
1
cat ~/.ssh/id_rsa.pub
  • 复制到Git控制台

  • 测试

1
ssh -T git@github.com

拉取仓库代码

clone

  • clone default branch
    • git clone https://github.com/golang/go.git
    • git clone git@github.com:golang/go.git
  • 指定分支
    • branch_name :是你要克隆的分支的名称
    • remote_repository_url :是远程仓库的 URL
    • git clone -b my-branch https://github.com/your-username/your-repo.git

pull

  • git pull origin master

提交代码

添加文件到暂存区

  • 指定文件:git add file_name
  • 当前目录所有:git add .
  • 模糊:git add *.go
  • 指定目录:git add src/
  • 交互模式:git add -i

commit

  • git commit -m “commit info”
  • git commit -m “标题” -m “正文”

push

  • git push
  • git push origin master
  • git push git@github.com:golang/go.git master

状态查询

  • git status
  • 更详细:git status -v
  • 其他分支差异:git status -b
  • 查看未跟踪的文件:git status -u
  • 查看更改预览:git status -s
  • 查看分支状态:git status -sb

日志

  • git log
  • 以简洁的方式显示每个提交的一行摘要信息:git log –oneline
  • 在输出中显示提交历史的图形化表示,用于展示分支和合并情况:git log –graph
  • 按作者筛选提交:git log –author=”Cc”
  • 按日期范围筛选提交:git log –since=”2023-01-01” –until=”2023-04-30”
  • 特定文件的提交历史:git log – file_name
  • 查看某个分支或标签的提交历史
    • git log branch_name
    • git log tag_name
  • 查看合并提交的历史:git log –merges
  • 查看非合并提交的历史:git log –no-merges
  • 显示某个提交的详细信息:git log commit_hash

对比

  • git diff [commit1] [commit2] – [file]
  • 比较暂存区与最新提交之间的差异:git diff –staged
  • 比较两个不同提交之间的差异:git diff commit1 commit2
  • 比较特定文件的修改:git diff file_name
  • 比较两个提交之间某个文件的修改:git diff commit1 commit2 – file_name
  • 比较某个分支或标签与当前分支之间的差异:git diff branch_or_tag
  • 查看以缓存的改的:git diff –cached [file]
  • 查看缓存状态摘要,默认全部 file 可以指定文件:git diff –stat [file]

撤回

  • git status
  • git log
  • git reset –hard [hash]
  • 强制删除丢弃保存修改:git reset –hard
  • 撤销最近一次提交,但保留更改:git reset –soft HEAD^
  • 取消最近一次提交并取消暂存的内容:git reset –mixed HEAD^
  • 彻底删除最近一次提交及其更改(慎用):git reset –hard HEAD^
  • 将分支引用移动到特定的提交:git reset commit_hash
  • 取消暂存,但保留工作目录的修改:git reset

分支

  • 列出远程所有分支:git branch -r
  • 列出所有分支:git branch -a
  • 创建新分支:git branch new_feature
  • 切换到新分支:git checkout new_feature
  • 创建并切换到新分支:git checkout -b hotfix_bug
  • 删除已合并的分支:git branch -d branch_name
  • 强制删除未合并的分支:git branch -D feature_in_progress
  • 重命名分支:git branch -m old_name new_name
  • 删除远程分支
    • git push origin –delete [branch-name]
    • git branch -dr [remote/branch]

      tag

This post is licensed under CC BY 4.0 by the author.