zh-rocco / fe-notes

:memo: 前端笔记

Home Page:https://zh-rocco.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

【Git】常用命令

zh-rocco opened this issue · comments

commented

生成 SSH keys

# 查看 SSH keys 是否存在
ls -al ~/.ssh

# 生成新的 SSH key
ssh-keygen -t rsa -C "rocco.mormont@gmail.com"

# 测试 SSH
ssh -T git@github.com

gerrit 多 SSH 配置

~/username/.ssh/config:

# gerrit
Host gerrit_host_url
HostName gerrit_host_url
# gerrit 对应的 email 或者用户名
User email
PreferredAuthentications publickey
# github对应的私钥
IdentityFile ~/.ssh/id_rsa_gerrit

查看/修改用户信息

# 查看
git config user.name
git config user.email

#修改
git config --global user.name "rocco"
git config --global user.email "rocco.mormont@gmail.com"

查看/创建/切换 分支

# 查看本地分支
git branch

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

# 新建分支
git branch <branchName>

# 切换分支
git checkout <branchName>

# 新建并切换至新建的分支
git checkout -b <branchName>

删除分支

# 删除本地的某个分支
git branch -D <branchName>

# 删除远程的分支
git branch -r -d origin/<branchName>
git push origin :<branchName>

# 解释:
# git branch -r -d origin/<branchName> 只是删除本地的索引,而不是真正删除远程分支的内容
# 要想真正删除远程分支上的内容,把一个空分支 push 到 server 上,等于删除该分支,git push origin :<branchName>
# 注意:冒号前面的空格不能少

撤销提交(复位)

# 该命令撤消上一个commit,但保留add的文件,Git 会暂存所有的因复位带来的差异,但不提交它们
git reset --soft HEAD^

# 强制复位前一个提交
git reset --hard HEAD^

参考

  1. Git 撤销提交和修改相关操作

清空当前分支

# 清空当前分支
# 注意不要遗漏 -r 后面的 .
git rm --cached -r .
git clean -f -d

# 创建空的 commit
git commit --allow-empty -m "[empty] initial commit"

# 推送空分支
git push

新建空分支

# 新建空分支
# 注意不要遗漏 -r 后面的 .
git branch -b <new_branch>
git rm --cached -r .
git clean -f -d

# 创建空的 commit
git commit --allow-empty -m "[empty] initial commit"

# 推送空分支
git push origin <new_branch>

修改文件名大小写

git config core.ignorecase true

git mv <source> <destination> # 重命名 "source" 为 "destination"

修改提交后的用户名和邮箱

#!/usr/bin/env bash

# set -ex

git filter-branch --env-filter '
    an="$GIT_AUTHOR_NAME"
    am="$GIT_AUTHOR_EMAIL"
    cn="$GIT_COMMITTER_NAME"
    cm="$GIT_COMMITTER_EMAIL"
    if [ "$GIT_COMMITTER_EMAIL" = "要修改的邮箱地址" ]
    then
        cn="想要改成的用户名"
        cm="想要改成的邮箱地址"
    fi
    if [ "$GIT_AUTHOR_EMAIL" = "要修改的邮箱地址" ]
    then
        an="想要改成的用户名"
        am="想要改成的邮箱地址"
    fi
    export GIT_AUTHOR_NAME="$an"
    export GIT_AUTHOR_EMAIL="$am"
    export GIT_COMMITTER_NAME="$cn"
    export GIT_COMMITTER_EMAIL="$cm"
'

删除 Git 仓库的所有提交记录

Checkout

git checkout --orphan latest_branch

Add all the files

git add -A

Commit the changes

git commit -am "commit message"

Delete the branch

git branch -D master

Rename the current branch to master

git branch -m master

Finally, force update your repository

git push -f origin master --set-upstream origin master

恢复误删的 git stash 记录

背景

使用 Sourcetree 贮藏工作现场后,误删了这个 stash

第一步:git fsck --unreachable

git-fsck 文档

查找所有的 unreachable 记录

第二步:git show <sha>

其中 sha 是记录的 key(如下面的:bb3cc162df1270fcabac0dec53effc8166b9563b)

命令行中 drop:

$ git stash drop
Dropped refs/stash@{0} (bb3cc162df1270fcabac0dec53effc8166b9563b)

Sourcetree 中 drop:

使用 Sourcetree 删除时,我们是不知道 sha 的,此时回到第一步 git fsck --unreachable

查找所有的 unreachable 记录,例如:

$ git fsck --unreachable

Checking object directories: 100% (256/256), done.
Checking objects: 100% (3730/3730), done.
unreachable tree 3f80ada1c908812f125a5869ca7f77429e42a8c8
unreachable tree 7a409b8150ff5533ab4d22ebfbec520d4ce0bf7d
unreachable blob 8600c5b5609fbb541a2aa492d4eb4ed72c1e43a0
unreachable blob ee006fdb6554149a9f6aa6c3ef3d21853292b44f
unreachable blob b0c14ad2a35149815261ade2f89f3cf8e9fa096c
unreachable blob 57c221b28812aec98f2c44ee226ae75325ef28f3
unreachable blob 2783aa80d2dcdb51ac8cfe2f0cdbfd813ac6bc15
unreachable blob 65439852e70c0649d605a8452d79b4e16d40dada
unreachable blob 4b0472cf3317cbe8a476abd35ead1db1f791ebb7
unreachable tree c8c4ae2a9580bf788b39bf9ad770cc50cdeec734
unreachable tree 178594b9b22143860b6894377bc72b5c0ecbf6ea
unreachable tree 2a45ce0eb08dac0efe095b65eb9e98e3b1e26625
unreachable commit 49863934d99402979b2f43abd77b05e5a19780f2

对所有 unreachable commit 开头的记录,依次执行 git show <sha>,查看是否为误删的提交,例如:

$ git show bb3cc162df1270fcabac0dec53effc8166b9563b

commit bb3cc162df1270fcabac0dec53effc8166b9563b
Merge: 2800dc1 d7625d4
Author: *** <***@***.com>
Date:   Thu Jul 5 11:43:10 2018 +0800

    WIP on dev: 2800dc1 style(pc): 样式修改

diff --cc src/common/filters/index.js
index ac9ea48,ac9ea48..1f72657
--- a/src/common/filters/index.js
+++ b/src/common/filters/index.js
@@@ -1,5 -1,5 +1,3 @@@
  import currency from './currency'

--export default {
--  currency
--}
++export default { currency }

第三步:git stash apply <sha>

找到误删的 stash 后,使用 git stash apply <sha> 即可恢复

参考

删除最后一次提交记录

git reset --hard HEAD~1
git push --force

修改最后一次提交记录

git reset commitId # (注: 不要带 --hard)到上个版本
git stash # 暂存修改
git push --force # 强制 push, 远程的最新的一次 commit 被删除
git stash pop # 释放暂存的修改, 开始修改代码
git add .
git commit -m "massage"
git push

Gerrit Push

#!/usr/bin/env bash

# set -ex

branch=$(git symbolic-ref --short -q HEAD)

if [[ ! -n "$1" ]]; then
    echo "You have not input a branch, use the current branch:" $branch
else
    branch=$1
fi

echo "push branch: $branch"

git push origin HEAD:refs/for/$branch

exit 0