Git 实用操作总结

Git 实用操作总结

GIT的配置

安装git

点击这里下载个 Git ,安装到你的机器上。 或者使用你喜欢的包管理器安装。如: Homebrewpacman 什么的来安装都可。

设置 name 和 email

安装完成后,需要在命令行中设置自己的 nameemail:

1
2
3
4
5
git config --global user.name "你的昵称"
git config --global user.email "你的邮箱"
举例:
git config --global user.name "wwxiaoqi"
git config --global user.email "tahicoing@gmail.com"

将远程和本地仓库绑定

git init

通过 git init 命令把目录变成 Git 管理仓库:

1
2
3
mkdir test
cd test
git init

地址绑定

后使用以下命令绑定地址:

SSH: 
git remote add origin git@github.com:wwxiaoqi/go-wing.git

HTTPS:
git remote add origin https://github.com/wwxiaoqi/go-wing.git

修改默认分支

并将默认分支修改为 main:

1
2
git branch -M main
git push -u origin main

Git 基本的工作模型

git clone

将仓库 clone 到本地(使用命令: git clone

1
2
3
git clone 地址
举例:
git clone https://github.com/wwxiaoqi/dwm

代码提交

把写完的代码提交(先用 git add 文件名 把文件添加到暂存区,再用 git commit 提交)

1
2
git add test.txt
git commit -m "修改了 test.txt 文件"

git status

git status 来随时查看工作目录的状态。文件有以下四种状态:

  • changed / unstaged(已修改)
  • staged(已修改并暂存)
  • commited(已提交)
  • untracked(未跟踪)

git push

本地提交 push 到中央仓库

1
git push

Git 的引用

HEAD 和 branch

  • HEAD 是当前 commit 的引用,每个仓库中只有一个 HEAD。每次提交自动向前移动 commit
  • branch 是一类引用。HEAD 除了直接指向 commit,还可以通过指向某个 branch 来间接指向 commit。当 HEAD 指向一个 branch 时,commit 发生时,HEAD 会带着它所指向的 branch 一起移动。

branch

branch 创建、切换、删除:

1
2
3
4
5
git branch branchName       创建分支
git checkout branchName     切换分支
git checkout -b branchName  创建并切换分支
git branch -d branchName    删除分支
git branch -D branchName    强制删除分支

Git Merge 合并

merge 的含义

从两个 commit 分叉的位置开始,把目标 commit 的内容应用到当前 commit,并生成一个新的 commit

merge 适用场景

  • 单独开发的 branch 用完了以后,合并回原先的 branch
  • git pull 的内部自动操作。

merge 的三种特殊情况

  1. 冲突 原因:当前分支和目标分支修改了同一部分内容,Git 无法确定应该怎样合并。 应对方法:解决冲突后手动 commit
  2. HEAD 领先于目标 commitGit 什么也不做。
  3. HEAD 落后于目标 commitfast-forward

Git 历史记录

1
2
3
4
5
6
7
8
9
git log           (查看多个历史)
git log -p        (查看详细改动)
git log --stat    (大致看改动内容)
git diff --staged  (比对暂存区和上一条提交)

如果你想看某个具体的 commit 的改动内容:
git show  
git show 68b85e0d
git show 68b85e0d test.txt  

Git 修复

commit --amend 修复当前提交的错误:

1
git commit --amend

Git 下载速度优化

代理

  1. HTTP 仓库 对于 HTTP 仓库地址,可以在终端里输入以下两行命令,为 Git 设置全局的 HTTP 代理,从而提高 git clone/pull 的速度。
1
2
3
4
5
6
7
8
git config --global http.proxy "http://127.0.0.1:7899"
git config --global https.proxy "https://127.0.0.1:7890"

git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

git config --global --unset http.proxy
git config --global --unset https.proxy

执行完后 ~/.git/config 会变成这样:

1
2
3
4
[http]
    proxy = http://127.0.0.1:8080
[https]
    proxy = http://127.0.0.1:8080

以后再 pull 代码就会走代理。

  1. SSH 仓库 对于这种仓库,可以在 ~/.ssh/config 设置代理,提高 git pull/push 的速度。
1
2
3
4
Host github.com
    User git
    HostName github.com
    ProxyCommand nc -v -x 127.0.0.1:7890 %h %p

修改 HOSTS

http://github.global.ssl.fastly.net.ipaddress.com/ 获取到速度比较快的 ip,通过替换 hosts ,达到加速的目的,例如 fliu2476/gh-hosts 这个项目,就是这个实现原理。

1
2
3
vim /etc/hosts

151.101.185.194 github.global.ssl.fastly.net

Git 其他问题

SSH HOST 连接失败

ssh:connect to host github.com port 22: Connection timed out

对于这种情况的解决方法是指定 443 端口,可以在 ~/.ssh/config 添加以下内容:

1
2
3
4
Host github.com
	User git
	Hostname ssh.github.com
	Port 443

其中 Port 443 是指定 443 端口。