Skip to main content

Git 笔记

用法举例


# 将当前文件夹初始化为 git 仓库
git init

# 将当前所有工作内容(增、删、改)添加至暂存区准备提交
git add --all

# 提交代码
git commit -m "对本次提交内容进行说明"

# 查看提交记录
git log
git log --oneline -10 # 单行显示记录,显示10条

# 查看某次提交详情
git show ccb8a98

# 查看远程仓库信息
git remote -v

# 增加远程分支
git remote add origin git@github.com:hycgh/dcloud.git

# 推送当前分支到远程仓库
git push

# 首次关联远程分支并推送
git push -u origin main

# 从远程仓库拉取更新
git pull

# 清除所有修改,恢复到最后一次提交的状态
git reset --hard

# 删除文件,加 --cache 只会从版本控制中移除不会从磁盘删除
git rm filename --cache

# 删除文件夹,加 --cache 只会从版本控制中移除不会从磁盘删除
git rm -r pathname --cache


########### 分支 ############
# 查看分支
git branch -v

# 查看所有分支
git branch -a

# 创建分支
git branch my-branch

# 修改当前分支名为 main
git branch -M main

# 切换分支
git checkout my-branch

# 创建并切换分支
git checkout -b my-branch

# 删除分支 , 大写 -D 可强制删除未合并分支
git branch -d my-branch


########### 配置 ############
# 查看所有当前生效的配置
git config --list

# 查看系统级配置
git config --system --list

# 查看全局级配置
git config --global --list

# 查看本地级配置
git config --local --list

# 查看特定配置项的值
git config --get user.name
git config --get user.email

# 配置用户名和邮箱 注意:此邮箱会关联代表 github 账号
git config --global user.name hycgh
git config --global user.email 960213661@qq.com


########### 别名 ############
# 常用命令定别名,可以少敲好多键盘,别名配置举例:
git config --global alias.p "pull --rebase"
# 以上配置后,执行 git p 就相当于执行 git pull --rebase

# 常用别名配置:
git config --global alias.p "pull --rebase"
git config --global alias.r "!f() { git push -u origin HEAD:`git config branch.$(git name-rev --name-only HEAD).merge | sed -e 's@refs/heads/@refs/for/@'`$1; }; f"
git config --global alias.l "log --oneline -20"
git config --global alias.co "checkout"
git config --global alias.a "add --all"
git config --global alias.ca "commit --amend --no-edit"
git config --global alias.sl "stash list"
git config --global alias.ss "stash save"
git config --global alias.sp "stash pop"
git config --global alias.sd "stash drop"
git config --global alias.rh "reset --hard"
git config --global alias.sk "stash save --keep-index"
git config --global alias.sa "stash apply"
git config --global alias.cl "clone --depth 1"
git config --global alias.cp "cherry-pick"
git config --global alias.c "commit -m"
git config --global alias.b "branch -v"
git config --global alias.s "status"
git config --global alias.p "pull --rebase"
git config --global alias.m "merge"
git config --global alias.t "push"
git config --global alias.lm "log --author='huangyongcheng' --oneline"
git config --global alias.ls "log --author='huangyongcheng' --pretty=format:'%s'"
git config --global alias.la "log --pretty=format:'%h %cd %an -- %s' --date=format:'%Y-%m-%d %H:%M' --all"
git config --global alias.lam "log --author='huangyongcheng' --pretty=format:'%cd - %s' --date=format:'%Y-%m-%d %H:%M' --all"
git config --global alias.lf "log --pretty=format:'%h %cd %an -- %s' --date=format:'%Y-%m-%d %H:%M'"
git config --global alias.rv "remote -v"

# 注意:别名不能和 git 原始命令重名!例如 git config --global alias.pull "pull --rebase" 是不会生效的

# 查看所有定义的别名,别名本质是 config ,所以以下命令是根据关键字筛选别名相关配置
git config --get-regexp alias

# 删除别名
git config --global --unset alias.st


########### 暂存 ############
# 暂存工作区修改(新增的文件不会暂存,若需要可以先 add)
git stash

# 暂存工作区修改并指定【暂存名称】,git stash list 时可以看到名称
git stash save name

# 恢复最后一次暂存的代码,并删除此条暂存(如果有冲突不会删除)
git stash pop

# 查看所有暂存
git stash list

# 恢复最后一次暂存的代码
git stash apply

# 应用第2条暂存
git stash apply 2

# 暂存未添加的修改,并保留暂存区的内容
git stash --keep-index


### 当本地有提交,远程仓库又有更新的情况下拉取代码 git 会自动创建一条合并提交记录
# 避免产生 merge commit
git pull --rebase


### 关联远程仓库
# 配置远程仓库,修改当前分支名为 main , 推送 main 分支到 origin 仓库
git remote add origin git@github.com:hycgh/quickopenpro.git
git branch -M main
git push -u origin main

生成密钥并配置 github

生成密钥

生成密钥时指定的邮箱与 github 账号无关

gitbash
ssh-keygen -o(废弃)
ssh-keygen -t ed25519 -C "522758851@qq.com/960213661@qq.com"
ssh-keygen -t ed25519 -C "960213661@qq.com"

配置 github 密钥

菜单 用户的 settings->ssh and gpg keys->new ssh key,把 id_rsa.pub(id_ed25519.pub) 文件的内容粘贴到输入框

针对仓库配置 Deploy Key 允许别人 pull 代码

菜单 具体仓库的 settings->deploy keys->add deploy key,把 id_rsa.pub(id_ed25519.pub) 文件的内容粘贴到输入框

报错解决

git pull 时报错 There is no tracking information for the current branch

需要将 origin 远程仓库的 main 分支与本地 main 分支关联

git branch --set-upstream-to=origin/main main

git pull 时报错 fatal: refusing to merge unrelated histories

git pull origin master --allow-unrelated-histories

3vjia git config --list

diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager-core
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=master
user.name=huangyongcheng
user.email=huangyongcheng@3vjia.com
url.ssh://huangyongcheng@gerrit.3weijia.com:29418/.insteadof=ssh://gerrit.3weijia.com:29418/
alias.r=!f() { git push -u origin HEAD:`git config branch.$(git name-rev --name-only HEAD).merge | sed -e 's@refs/heads/@refs/for/@'`$1; }; f
alias.l=log --oneline -20
alias.co=checkout
alias.a=add --all
alias.ca=commit --amend --no-edit
alias.sl=stash list
alias.ss=stash save
alias.sp=stash pop
alias.sd=stash drop
alias.rh=reset --hard
alias.sk=stash save --keep-index
alias.sa=stash apply
alias.cl=clone --depth 1
alias.cp=cherry-pick
alias.c=commit -m
alias.b=branch -v
alias.s=status
alias.p=pull --rebase
alias.m=merge
alias.t=push
alias.lm=log --author='huangyongcheng' --oneline
alias.ls=log --author='huangyongcheng' --pretty=format:'%s'
alias.la=log --pretty=format:'%h %cd %an -- %s' --date=format:'%Y-%m-%d %H:%M' --all
alias.lam=log --author='huangyongcheng' --pretty=format:'%cd - %s' --date=format:'%Y-%m-%d %H:%M' --all
alias.lf=log --pretty=format:'%h %cd %an -- %s' --date=format:'%Y-%m-%d %H:%M'
alias.rv=remote -v
http.emptyauth=true
http.sslverify=false
core.longpaths=true

3vjia .gitconfig

C:\Users\huangyongcheng\.gitconfig

[user]
name = huangyongcheng
email = huangyongcheng@3vjia.com
[url "ssh://huangyongcheng@gerrit.3weijia.com:29418/"]
insteadOf = ssh://gerrit.3weijia.com:29418/
[alias]
r = "!f() { git push -u origin HEAD:`git config branch.$(git name-rev --name-only HEAD).merge | sed -e 's@refs/heads/@refs/for/@'`$1; }; f"
l = log --oneline -20
co = checkout
a = add --all
ca = commit --amend --no-edit
sl = stash list
ss = stash save
sp = stash pop
sd = stash drop
rh = reset --hard
sk = stash save --keep-index
sa = stash apply
cl = clone --depth 1
cp = cherry-pick
c = commit -m
b = branch -v
s = status
p = pull --rebase
m = merge
t = push
lm = log --author='huangyongcheng' --oneline
ls = log --author='huangyongcheng' --pretty=format:'%s'
la = log --pretty=format:'%h %cd %an -- %s' --date=format:'%Y-%m-%d %H:%M' --all
lam = log --author='huangyongcheng' --pretty=format:'%cd - %s' --date=format:'%Y-%m-%d %H:%M' --all
lf = log --pretty=format:'%h %cd %an -- %s' --date=format:'%Y-%m-%d %H:%M'
rv = remote -v
[http]
emptyAuth = true
sslverify = false
[core]
longpaths = true