Git 命令集

Git flow

Git 指令使用手册

常用指令

初始化一个空的 Git 仓库 demo

克隆项目基本操作

分支操作

Feature branching 工作流 - 方式1-靠吼

Feature branching 工作流 - 方式2 - Pull Request

说明:在 Git 中,有两个「偏移符号」: ^ 和 ~。

^ 的用法:在 commit 的后面加一个或多个 ^ 号,可以把 commit 往回偏移,偏移的数量是 ^ 的数量。例如:master^ 表示 master 指向的 commit 之前的那个 commit; HEAD^^ 表示 HEAD 所指向的 commit 往前数两个 commit。

~ 的用法:在 commit 的后面加上 ~ 号和一个数,可以把 commit 往回偏移,偏移的数量是 ~ 号后面的数。例如:HEAD~5 表示 HEAD 指向的 commit往前数 5 个 commit。

Rebase 816f737..b13037a onto 816f737 (1 command)

Commands: p, pick = use commit r, reword = use commit, but edit the commit message e, edit = use commit, but stop for amending s, squash = use commit, but meld into previous commit f, fixup = like "squash", but discard this commit's log message x, exec = run command (the rest of the line) using shell d, drop = remove commit

These lines can be re-ordered; they are executed from top to bottom.

If you remove a line here THAT COMMIT WILL BE LOST.

However, if you remove everything, the rebase will be aborted.

Note that empty commits are commented out

撤销commit

撤销已在中央仓库的 branch 的 commit

撤销已在中央仓库的 master 的 commit, 只是新增一条 revert commit

如果出错内容在私有 branch:在本地把内容修正后,强制 push (push -f)一次就可以解决;

如果出错内容在 master:不要强制 push,而要用 revert 把写错的 commit 撤销。

重置回退

--hard:重置位置的同时,清空工作目录的所有改动;

--soft(常用):重置位置的同时,保留工作目录和暂存区的内容,并把重置 HEAD 的位置所导致的新的文件差异放进暂存区。

--mixed(默认):重置位置的同时,保留工作目录的内容,并清空暂存区。

Checkout

Stash(暂存区): 临时存放工作目录的改动

重建已删除的 branch1

tag

cherry-pick

patch: 对已有的 commit 记录生成补丁,并应用补丁

Pull changes from a template repository

更改远程仓库 URL

git config

Learn Git Branching - 速查

Git Remotes

Push & Pull

To Origin And Beyond

Branch

Delete local branch

Delete remote branch

Git Subtree

迁移仓库

  • 在新的 Git 仓库上新建项目xxx;

  • 单独克隆一下旧仓库地址,包含所有提交记录

  • 推送到新的仓库地址

  • 切换旧项目的 remote url

提交信息类别

git commit -m "fix: xxxxxxxx"

Questions: 常见问题

  • SSL_read: SSL_ERROR_SYSCALL, errno 10054

  • OpenSSL SSL_read: Connection was reset, errno 10054

Solutions:

  • Couldn't connect Github on your network, need use VPN;

  • Update DNS cache;

    • Mac: sudo killall -HUP mDNSResponder sudo dscacheutil -flushcache

    • Windows: ipconfig /flushdns

  • remote: HTTP Basic: Access denied fatal: Authentication failed for 'Your remove url'

方案3 情况:第一次输入账号密码错误后,或者 git 账号更新了密码, 或者 Git 管理员强制用户更新密码; 方案3 方法:到”控制面板 \ 用户帐户 \ 凭据管理器 \ Windows凭据”下找到对应 git 的凭据,1. 直接修改凭据里的账号密码; 2. 删除凭据,然后继续尝试 git clone 命令,会重新提示输入用户名和密码;

  • Github, Gitlab, Gitee, 多账号管理 - 提交时的用户选择

  • Could not merge origin/master: You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before you merge.

  • Committing is not possible because you have unmerged files.

  • Your local changes to the following files would be overwritten by merge: Please commit your changes or stash them before you merge.

  • git merge master fatal: refusing to merge unrelated histories

  • Warning: LF will be replaced by CRLF

  • npm install: Error: Can't find Python executable "python", you can set the PYTHON env variable

  • 分支 Merge 后仍然提示:有未合并文件

commit is not possible because you have unmerged files

  • Rebasing master with "git pull --rebase"

  • git pull - error: You have not concluded your merge (MERGE_HEAD exists).

hint: Please, commit your changes before merging. fatal: Exiting because of unfinished merge.

  • Please move or remove them before you switch branches. Aborting fatal: Could not detach HEAD First, rewinding head to replay your work on top of it

手动删除或添加哪些未被添加到 git 的本地文件,然后再 git pull

  • Squash commits in git after they have been pushed?

Such as squash last 4 commits in remote master

  • Failed to connect to github.com port 443: Timed out

fatal: unable to access 'https://github.com/gyx8899/blog.git/': Failed to connect to github.com port 443: Timed out

检查网络状态,发现并解决网络问题,或重连(启)网络连接,重新尝试 git 命令即可。

  • Pull request 提交后, 如果 diff 文件被 revert 并提交,会导致 pull request 自动关闭

注意确保修改文件的改动在分支上,然后再建分支进行进一步测试。而不是直接在同一个分支或 Master 上修改提交,这会对当前的 pull request 造成影响,因为这些后续改动也会被包含在 pull request 中。

  • "fatal: ambiguous argument 'HEAD" after "git reset --hard HEAD^" in windows

^在 windows (terminal) 中是特殊字符。

Git 的每个Commit 都有作者(Author)跟提交者(Committer)两种角色,每次新增修改删除档案并使用git commit 指令存成Commit,一开始Commit 的作者与提交者都是执行提交动作的操作人员(严格来说是user.name 跟user.email 所设定的身分),而作者日期(AuthorDate)及提交日期(CommitDate)就是执行git commit 的时间。但如果Commit 经过再处理或操作,提交日期将会更新,而也可能出现提交者与作者不同的状况。造成作者/作者日期与提交者/提交日期不同的常见情境有:

  • 执行Rebase (包含git pull --rebase)

  • 执行Amend 修改Commit 讯息

  • 执行Cherry-Pick 搬动Commit

  • 产生更新档交付他人套用

总之,只要Git 操作导致Commit ID 改变,就必须更新提交者及提交日期,若操作者并非该Commit 的原始提交者,便会发生作者与提交者不同的状况。要观察提交日期与提交者,除使用Visual Studio、Source Tree、Git GUI 等GUI 工具,用git show --pretty=fuller commit_id 亦可查看

  • 修改 branch 的名字

  • 使用 git 命令中出现 - remote: Repository not found.

fatal: repository 'https://github.com/xxxxxx/xxxxxx.git/' not found

此处的情况有:

  1. 提交另一个 github 账号下的仓库改动,但是这个仓库是私有的。而本地 git 记录的账号密码不是这个 github 账号。

  2. fatal: unable to access 'https://github.com/[user-name]/[repo-name].git/': The requested URL returned error: 403

解决方法:

"\346\265\213\350\257\225.txt"

Git中文显示问题解决

  • fatal: not a git repository (or any of the parent directories): .git

git 命令只能在 git 目录中使用,执行:git init

  • Failed to connect to github.com port 443:connection timed out

git config --global --unset http.proxy

git config --global --unset https.proxy

  • You have unstaged changes.

  • remote: Permission to gyx8899/blog.git (Owned by Git-User-A) denied to [Git-User-B].

  • npm install error: node-sass 3.13.1

参考 https://blog.csdn.net/y_k_y/article/details/86535268 文章中最后的方案,即 https://github.com/pnp/sp-dev-fx-webparts/issues/501#issuecomment-389285371 方案,以管理员权限安装 npm 包 - windows-build-tools,成功后重新 npm install。问题即可解决。

示例执行的命令:

在 VS code 的 terminal 中执行(测试成功)

  • npm install nrm error: internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name, ‘string’, value);

  • warning: LF will be replaced by CRLF

  • Your branch is based on 'origin/master', but the upstream is gone.

  • fatal: The current branch master has no upstream branch.

  • error /node_modules/node-sass: Command failed (npm install in New Mac)

情况之一:新电脑安装的 Node version 太高了,node-sass 不匹配,需要安装 nvm 使用低版本的 node

  • fatal: 'https://github.com/gyx8899/blog.git/' 鉴权失败

remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead. remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.

stackoverflow 方案

  • Git: hint: You have divergent branches and need to specify how to reconcile them. 提交了本地代码未推送,同时 remote 有新改动未拉到本地,push 本地代码时报错提示

    warning: 不建议在没有为偏离分支指定合并策略时执行pull操作。您可以在执行下一次pull操作之前执行下面一条命令来抑制本消息:

    git config pull.rebase false # 合并(默认缺省策略)

    git config pull.rebase true # 变基

    git config pull.ff only # 仅快进

    您可以将 "git config" 替换为 "git config --global" 以便为所有仓库设置缺省的配置项。您也可以在每次执行 pull 命令时添加 --rebase --no-rebase,或者 --ff-only 参数覆盖缺省设置。

  • Git 全局忽略文件

  • Git 重置分支为 master

  • Git 撤销 rebase 引起的变基

  • Git merge 后 push 阶段提示校验不通过

练习

参考

最后更新于

这有帮助吗?