git 咋用啊

日出江花红胜火

查看操作记录

git reflog

提交错分支

方案 1

先新建一个分支

git branch new_branch

取消提交的分支

git reset HEAD~ --hard

方案 2

先撤销最后一次提交, 但保留变更

git reset HEAD~ --soft

git stash

再切换到正确的分支, 提交代码

git checkout correct_branch

git stash pop

git add .

git commit -m "msg"

方案 3

cherry-pick

先切换到正确的分支

git checkout correct_branch

cherry-pick 获取最新一条记录

git cherry-pick master

删除 master 错误的记录

git checkout master

git reset HEAD~ --hard

账号密码

设置单独的 git 配置

在 .git 路径下执行, 不要带 —global参数

1
2
$ git config user.name "B"
$ git config user.email "B@gmail.com"

自动记录账号密码

git config --global credential.helper store

添加远程仓库

git remote add go_turorial git@gitee.com:afreto/go_tutorial.git

拉取远程分支到本地分支

git checkout --track origin/feat/5.01

不带 —track 参数只会拉取 index

git config --global branch.autosetuprebase always

回滚

git reset --soft HEAD^

HEAD^ 的意思是上一个版本,也可以写成 HEAD~1

如果你进行了 2 次 commit,想都撤回,可以使用 HEAD~2

–mixed
意思是:不删除工作空间改动代码,撤销 commit,并且撤销 git add . 操作
这个为默认参数,git reset –mixed HEAD^ 和 git reset HEAD^ 效果是一样的。

–soft
不删除工作空间改动代码,撤销 commit,不撤销 git add .

–hard
删除工作空间改动代码,撤销 commit,撤销 git add .

注意完成这个操作后,就恢复到了上一次的 commit 状态。

回滚到 index

git reset HEAD@{index}

添加改动到最后一次 commit

也可用来重新提交 commit message

git commit --amend

配置代理

1
2
3
4
5
6
7
8
9
10

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

git config --global http.proxy http://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 remote show origin

从本地版本库中删除

git remote prune origin

查看 git 个人代码量

git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

替换一下 username