Git效率与工具

git最佳实践

2018-06-12  本文已影响13人  奇而思

1. 本地设置

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

2. 以下分为两种情况:

3. 冲突

在合并时会出现冲突,例如提示:

Auto Merge Failed; Fix Conflicts and Then Commit the Result.

这时需要手动解决冲突,并提交。
首先,使用git status 分析原因,是哪个文件出现了冲突。例如结果显示如下:

> root@hyk-virt:/etc# git status
> # On branch master
> # Your branch and 'origin/master' have diverged,
> # and have 2 and 2 different commits each, respectively.
> #
> # Unmerged paths:
> #   (use "git add/rm <file>..." as appropriate to mark > resolution)
> #
> #    both modified:      apt/sources.list
> #
> # Changes not staged for commit:
> #   (use "git add <file>..." to update what will be > committed)
> #   (use "git checkout -- <file>..." to discard changes > in working directory)
> #
> #    modified:   cups/subscriptions.conf
> #    modified:   cups/subscriptions.conf.O
> #    modified:   mtab
> #    modified:   update-manager/release-upgrades
> #
> no changes added to commit (use "git add" and/or "git > commit -a")

可以看到是sources.list文件出现了冲突。这时候,可以使用文本编辑器打开该文件。冲突产生后,冲突文件会显示以下标记<<<<<<<=======之间是本地修改的内容,=======>>>>>>>之间是远程修改的内容。
根据这个,对冲突文件进行编辑,在修改完之后,
git add filename
git commit
重新commit以下就可以了。

方法二

如果我们确定远程的分支正好是我们需要的,而本地的分支上的修改比较陈旧或者不正确,那么可以直接丢弃本地分支内容,运行如下命令(看需要决定是否需要运行git fetch取得远程分支):

$:git reset --hard origin/master

或者$:git reset --hard ORIG_HEAD

git-reset - Reset current HEAD to the specified state

--hard
Resets the index and working tree. Any changes to tracked files
in the working tree since <commit> are discarded.

方法三

如果我们觉得合并以后的文件内容比价混乱,想要废弃这次合并,回到合并之前的状态,那么可以运行如下命令:
git reset --hard HEAD

分支

如果想把本地的某个分支test提交到远程仓库,并作为远程仓库的master分支,或者作为另外一个名叫test的分支,那么可以这么做。

$ git push origin test:master // 提交本地test分支作为远程的master分支
$ git push origin test:test // 提交本地test分支作为远程的test分支

如果想删除远程的分支呢?类似于上面,如果:左边的分支为空,那么将删除:右边的远程的分支。

$ git push origin :test // 刚提交到远程的test将被删除,但是本地还会保存的,不用担心

上一篇下一篇

猜你喜欢

热点阅读