mileOfSunshine / blog

2 stars 0 forks source link

常用的 git 指令 #1

Open mileOfSunshine opened 5 years ago

mileOfSunshine commented 5 years ago
mileOfSunshine commented 5 years ago

Bug 修复时,合并 commit 方式

  1. git commit --fixup

  2. git rebase --autosquash

fle github io_git-tip-keep-your-branch-clean-with-fixup-and-autosquash html

详见:GIT tip : Keep your branch clean with fixup and autosquash

mileOfSunshine commented 5 years ago

两种合并 commits 方式

  1. 方式1
git rebase -i master # 会出现一个互动界面

# pick: 选中
# reword: 选中,并且修改提交信息
# edit: 选中,rebase时会暂停,允许你修改这个commit
# squash: 选中,会将当前commit与上一个commit合并
# fixup: 与squash相同,但不会保存当前commit的提交信息
# exec: 执行其他shell命令

git push --force origin master
  1. 方式2

先撤销过去2个 commit , 然后新建一个新的。 有时候使用 rebase 合并会遇到一些 permission denied 问题,若不知如何解决可采用此种合并方式.

git reset HEAD~2
git add .
git commit -m 'commits'
git push --force origin master
  1. 方式3

合并上一次 commit

git add .
git commit --amend
git push -f origin master
mileOfSunshine commented 5 years ago

版本回退

版本回退是指将版本库从当前版本回退到其他的版本。


git log # 查看日志

git reset --hard 版本号

# 在 Git 中, HEAD 指针指向的是当前版本, 也就是最新的 commit id, 上一个版本是 HEAD^, 上上一个版本就是 HEAD^^, 上50个版本可以写成 HEAD~50。
mileOfSunshine commented 5 years ago

远程仓库已经删除的分支在本地依然可以看到

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

运行的结果是为何远程已经删除的分支也显示了。。。??

git remote show origin # 可以查看远程分支,本地分支,以及对应关系

运行结果里标出了远程仓库不存在的分支,并提示可使用 git remote prune 移除。

git remote prune origin #可移除远程不存在的分支
mileOfSunshine commented 5 years ago

撤销本地commit

git reset HEAD~
mileOfSunshine commented 5 years ago

写好 git commit

mileOfSunshine commented 5 years ago

lerna 相关文章

https://github.com/hardfist/stackoverflow/issues/24

mileOfSunshine commented 4 years ago

更改本地和远程分支的名称


git branch -m old_branch new_branch # Rename branch locally 

git push origin :old_branch # Delete the old branch 

git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
mileOfSunshine commented 4 years ago

git 提交时 CRLF 和 LF 问题

warning: LF will be replaced by CRLF fatal: CRLF would be replaced by LF

原因:Git的换行符检查功能

core.safecrlf

Git提供了一个换行符检查功能(core.safecrlf),可以在提交时检查文件是否混用了不同风格的换行符。这个功能的选项如下:

false: 不做任何检查 warn:在提交时检查并警告 true: 在提交时检查,如果发现混用则拒绝提交

core.autocrlf

假如你正在Windows上写程序,又或者你正在和其他人合作,他们在Windows上编程,而你却在其他系统上,在这些情况下,你可能会遇到行尾结束符问题。这是因为Windows使用回车和换行两个字符来结束一行,而Mac和Linux只使用换行一个字符。虽然这是小问题,但它会极大地扰乱跨平台协作。

Git可以在你提交时自动地把行结束符CRLF转换成LF,而在签出代码时把LF转换成CRLF。用core.autocrlf来打开此项功能,如果是在Windows系统上,把它设置成true,这样当签出代码时,LF会被转换成CRLF:

$ git config --global core.autocrlf true

Linux或Mac系统使用LF作为行结束符,因此你不想 Git 在签出文件时进行自动的转换;当一个以CRLF为行结束符的文件不小心被引入时你肯定想进行修正,把core.autocrlf设置成input来告诉 Git 在提交时把CRLF转换成LF,签出时不转换,这样会在Windows系统上的签出文件中保留CRLF,会在Mac和Linux系统上,包括仓库中保留LF。

$ git config --global core.autocrlf input

如果你是Windows程序员,且正在开发仅运行在Windows上的项目,可以设置false取消此功能,把回车符记录在库中:

$ git config --global core.autocrlf false
mileOfSunshine commented 4 years ago

推送本地分支代码到指定分支

git push <远程主机名> <本地分支名>:<远程分支名>
mileOfSunshine commented 3 years ago

批量删除本地分支

删除当前分支外的所有分支

 git branch | xargs git branch -d

删除分支名包含指定'dev'的分支

git branch | grep 'dev' | xargs git branch -d
mileOfSunshine commented 3 years ago

windows 下git配置 ssh

  1. 打开 vscode / gitbash
  2. cd ~/.ssh
  3. ssh-keygen -t rsa -C '【邮箱】'(一路回车),会生成 id_rsa (密钥), id_rsa.pub(公钥)
  4. Gitlab > Settings > SSH Keys > 输入 id_rsa.pub 内容 > Add key
  5. 清除旧的公钥信息,访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts
    ssh-keygen -R 【IP/域名】
  6. 回到 vscode / gitbash,执行 ssh -T git@【域名】,该操作会往 ~/.ssh/known_hosts 文件写入相关rsa信息
  7. 成功之后,就可以进行 git clone git@域名:xxxx/xxxxxx.git
mileOfSunshine commented 3 years ago

Git 工具 - 子模块

子模块允许你将一个 Git 仓库作为另一个 Git 仓库的子目录。 它能让你将另一个仓库克隆到自己的项目中,同时还保持提交的独立。

添加子模块

git submodule add  仓库地址  [路径]
# 例如
# git submodule add  https://github.com/mileOfSunshine/document-shell.git res/document-shell

执行完成,会在当前工程根路径下生成 .gitmodules,该配置文件保存了项目 URL 与已经拉取的本地目录之间的映射。需要注意的是,该文件也像 .gitignore 文件一样受到(通过)版本控制。

# .gitmodules
[submodule "res/document-shell"]
  path = res/document-shell
  url = https://github.com/mileOfSunshine/document-shell.git

检出

克隆一个包含子仓库的仓库目录,并不会clone下子仓库的文件,只是会克隆下.gitmodule描述文件,需要进一步克隆子仓库文件。

# 初始化本地配置文件
git submodule init

# 检出父仓库列出的commit
git submodule update

# OR 使用组合指令
 git submodule update --init --recursive

删除

mileOfSunshine commented 3 years ago

全局 commit 规范

# 全局安装commitizen
npm install -g commitizen
# 全局安装模版
npm install -g cz-conventional-changelog 
# for macos
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
# for anySysterm 手动创建.czrc,添加以下内容
{ "path": "cz-conventional-changelog" }
mileOfSunshine commented 2 years ago

打标签

git tag <lightweght_name>:为当前分支所在的提交记录打上轻量标签。

git tag <lightweght_name> <commit SHA-1 value>:为某次具体的提交记录打上轻量标签。

git tag -a <anotated_name> -m <tag_message>:为当前分支所在的提交记录打上附注标签。

git tag:列出所有的标签名。

git tag -d <tag_name>:删除某个标签,本质上就是移除 .git/refs/tags/ 中对应的文件。

git show <tag_name>:显示标签对应提交记录的具体信息。

git push <remote> <tag_name>:推送某个标签到远程仓库。

git push <remote> --tags:推送所有标签到远程仓库。

git push <remote> --delete <tag_name>:删除远程仓库中的某个标签。