git操做方法及常見問題合集

最近爲了使用git進行項目管理,整理了一些git相關的操做命令與常見問題合集。php


git操做指令blog

git教程-廖雪峯css

常見問題合集

若是系統中有一些配置文件在服務器上作了配置修改,而後後續開發又新添加一些配置項的時候,java

在發佈這個配置文件的時候,會發生代碼衝突:git

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.

若是但願保留生產服務器上所作的改動,僅僅併入新配置項, 處理方法以下:github

git stash
git pull
git stash pop

而後可使用Git diff -w +文件名 來確認代碼自動合併的狀況.服務器

反過來,若是但願用代碼庫中的文件徹底覆蓋本地工做版本. 方法以下:app

git reset --hard
git pull

其中git reset是針對版本,若是想針對文件回退本地修改,使用ide

git checkout HEAD file/to/restore

在push代碼時出錯:svn

$ 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衝突致使的。post

有以下幾種解決方法:

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後造成的引用。
相關文章
相關標籤/搜索