最近为了使用git进行项目管理,整理了一些git相关的操作命令与常见问题合集。
git操作指令blog
常见问题合集
如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候,
在发布这个配置文件的时候,会发生代码冲突:
error: Your local changes to the following files would be overwritten by merge:
protected/config/main.php
Please, commit your changes or stash them before you can merge.
如果希望保留生产服务器上所做的改动,仅仅并入新配置项, 处理方法如下:
git stash
git pull
git stash pop
然后可以使用Git diff -w +文件名 来确认代码自动合并的情况.
反过来,如果希望用代码库中的文件完全覆盖本地工作版本. 方法如下:
git reset --hard
git pull
其中git reset是针对版本,如果想针对文件回退本地修改,使用
git checkout HEAD file/to/restore
在push代码时出错:
$ git push -u origin master
To git@github.com:******/Demo.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:******/Demo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
是因为远程repository和我本地的repository冲突导致的。
有如下几种解决方法:
1.使用强制push的方法:
$ git push -u origin master -f
这样会使远程修改丢失,一般是不可取的,尤其是多人协作开发的时候。
2.push前先将远程repository修改pull下来
$ git pull origin master
$ git push -u origin master
3.若不想merge远程和本地修改,可以先创建新的分支:
$ git branch [name]
然后push
$ git push -u origin [name]
Pull is not possible because you have unmerged files.
在git pull的过程中,如果有冲突,那么除了冲突的文件之外,其它的文件都会做为staged区的文件保存起来。
重现:
$ git pull
A Applications/Commerce/BookingAnalysis.java
A Applications/Commerce/ClickSummaryFormatter.java
M Applications/CommerceForecasting/forecast/Forecast.java
A Applications/CommerceForecasting/forecast/ForecastCurveProviderCategory.java
M Applications/CommerceForecasting/forecast/ForecastProvider.java
M Applications/CommerceForecasting/forecast/InputPropertyItem.java
......
A Applications/LocalezeImporter/com/tripadvisor/feeds/SingleMenuLocalezeMatcher.java
A Applications/LocalezeImporter/com/tripadvisor/feeds/TypeCategory.java
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'.
通过git status你会发现下面古怪的事情:
zhonghua@pts/ttys000 $ git status
# On branch sns
# Your branch and 'snsconnect/sns' have diverged,
# and have 1 and 52 different commit(s) each, respectively.
#
# Changes to be committed:
#
# new file: src/config/features_daodao.ini
# new file: src/config/services.xml
# new file: src/config/svnroot/hooks/mailer.conf
# new file: src/config/svnroot/hooks/mailer.py
# new file: src/config/svnroot/hooks/post-commit
# new file: src/config/svnroot/hooks/pre-commit
# new file: src/config/svnroot/hooks/prerelease_notifications.py
# new file: src/config/svnroot/hooks/run_checks.py
…….
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# _build/
# css/combined/
# css/gen/
# daodao-site.patch
# daodao-site.patch1
# js/combined/
# js/gen/
# lib/weibo/
# src/bin/
Pull is not possible because you have unmerged files.
解决:
1. pull会使用git merge导致冲突,需要将冲突的文件resolve掉 git add -u, git commit之后才能成功pull.
2. 如果想放弃本地的文件修改,可以使用git reset --hard FETCH_HEAD,FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull.
注意:
3. git merge会形成MERGE-HEAD(FETCH-HEAD) 。git push会形成HEAD这样的引用。HEAD代表本地最近成功push后形成的引用。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。