Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git 常用命令 #2

Open
fi3ework opened this issue Feb 4, 2018 · 0 comments
Open

Git 常用命令 #2

fi3ework opened this issue Feb 4, 2018 · 0 comments
Labels

Comments

@fi3ework
Copy link
Owner

fi3ework commented Feb 4, 2018

作为一个基本上只会 push 和 pull 的还依赖于 SourceTree 的 git 弱渣,随手记录一下自己主要使用的 git 命令,让自己以后找起来更方便🙄

修正最后一个 commit 消息

场景:你在最后一条 commit 消息里有个笔误,已经执行了 git commit -m "Fxies bug #42",但在git push 之前你意识到消息应该是 “Fixes bug #42″。

方法git commit --amendgit commit --amend -m "Fixes bug #42"

详细git commit --amend 会用一个新的 commit 更新并替换最近的 commit ,这个新的 commit 会把任何修改内容和上一个 commit 的内容结合起来。如果当前没有提出任何修改,这个操作就只会把上次的 commit 消息重写一遍。

修正非最后一个 commit 消息

场景:突然发现之前的一次或几次commit不是十分满意,想要撤销或修改这条commit记录

方法git rebase -i HEAD~X

详细git rebase 可以用来修改历史的 commit 记录,包括压缩快进等。也可以用来修改 commit message,在需要修改 commit message 前修改为 er 即可。

参考:How to modify existing, unpushed commits?

撤销最后一个commit

场景:在完成了一次本地commit之后,对这次commit不是十分满意,想要撤销这条commit记录

方法git reset <last good SHA>git reset --hard <last good SHA>

详细:git reset 会把你的代码库历史返回到指定的 SHA 状态。 这样就像是这些提交从来没有发生过。缺省情况下,撤销的 commit 的修改的代码将会恢复到工作区,对应的 commit 的记录被撤销掉。如果加了--hard,那么撤销的 commit 的代码也会消失掉。如果想一并撤销远端的提交,使用 git push origin master --force 或者 git push -f origin master 即可,sourceTree 里也有开启 force push 的选项。

参考Force “git push” to overwrite remote files

合并远端新建的的分支

场景:在本地开了一个仓库并且有了一些 commit,同时在 Github 上也开了一个仓库,这种情况下是无法直接 pull 将两个仓库同步的,会报错 fatal: refusing to merge unrelated histories

方法git pull origin master --allow-unrelated-histories

详细:要合并的两个仓库没有共同的节点,相当于是要将两段不同的 history 合并,默认情况下是不允许的,需要添加 --allow-unrelated-histories

参考

合并多次commit

场景: 将多次提交的commit合并为一次提交。

方法

使用 worktree 在不同目录下同步

场景:在 master 分支下我们是开发环境,里面有 node_modules,这个文件夹在 gitignore 中当然是被忽略的的。然后我们在 gh-pages 中要做一个展示分支,放一些静态资源。那么每次当我们切换到 gh-pages 再切回 master 下时,master 下的 node_modules 就不!见!了!(因为没有被 track),导致还要重新 npm install 一次。

方法

  • 如果你已经存在了一个 gh-pages 分支,那么 git worktree add ../temp gh-pages
  • 如果还不存在要切换到的分支,那么 git worktree add ../temp -b gh-pages

然后我们就可以在 temp 文件中中随意操作,任何操作都会和原来的仓库保持同步。

commit 后,即可删除掉这个 temp 文件夹,然后执行 git worktree prune 清理一下 linked working tree 记录即可。

详细:git worktree 可以将分支切换到其他文件夹下,原来的文件夹叫做 main working tree,其他文件夹又叫做 linked working tree, linked working tree 已经完成了 git init 和 git clone 的操作,在 linked working tree 会被自动合并到 main linked working tree。

参考

@fi3ework fi3ework reopened this May 5, 2018
@fi3ework fi3ework changed the title Git常用操作 Git 常用命令 Jul 27, 2018
@fi3ework fi3ework added the Git label Jul 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant