一.讓.gitignore文件生效
1.git要排除一些文件或者文件夾,比如我的項目用phpstrom開發,會有一個.idea目錄,需要忽略。如果.idea目錄還沒有加入到git,則可以直接在.gitignore文件添加,但是如果.idea目錄已經在版本管理了,修改.gitignore就不會生效了,需要執行下面命令
git update-index --assume-unchanged .idea/*.xml
對於“.gitignore”文件,官方是這樣介紹的:
gitignore - A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.(gitignore文件指定了Git應該忽略的有意未跟蹤的文件。已經通過Git跟蹤的文件不受影響)
還有一種方式,讓修改後的.gitignore文件生效
git rm -r --cached .
git add .
git commit -m "update .gitignore"
二.git pull衝突解決
2.git stash save "保存的本地修改" 保存本地的修改到stash
有時候本地做了修改,遠程倉庫也做了修改,執行git pull的時候就會報錯
$ git pull
error: Your local changes to the following files would be overwritten by merge:
.gitignore
.idea/workspace.xml
Please commit your changes or stash them before you merge.
Aborting
Updating 5af7fb0..222f517
這時候就要把本地的修改保存到stash中。
先執行git stash save "我的修改".
再執行git pull
3.合併本地和遠程的修改,git stash pop會把最後一個stash的修改和剛纔pull拉下來的版本進行合併,如果有衝突會顯示出來
$ git stash pop
Auto-merging .idea/workspace.xml
CONFLICT (content): Merge conflict in .idea/workspace.xml
Auto-merging .gitignore
The stash entry is kept in case you need it again.
衝突顯示.idea/workspace.xml衝突,git會把兩個版本的衝突信息都寫到這個文件中,你需要編輯衝突文件
4.手動編輯文件,解決衝突
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
比如這是一個衝突 <<<<<< HEAD 到=====部分是遠程版本的修改, =====到>>>>>是本地版本的修改,你需要編輯需要留下的代碼,並且把這些標識刪除。
5.編輯好衝突文件,然後git add
git add .idea/workspace.xml
git commit -m "解決workspace.xml衝突"
git push
6.這就解決了
三,撤銷修改 git add之前撤銷修改(就是撤銷工作區的修改)
1.先看看有哪些文件修改過 git status,紅色的就是git add之前修改的文件
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
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: public/js/app.js
modified: resources/js/components/global/Navigation.vue
2.git checkout 文件名,就會撤銷修改,回到上一次git add時的狀態
$ git checkout public/js/app.js
$ git checkout resources/js/components/global/Navigation.vue
3.這是你git status發現,這兩個紅色的文件沒有顯示了
四,撤銷修改 git add之後 (就是撤銷暫存區的內容)
1.先看修改的文件 git status,綠色的就是git add後的文件
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: resources/js/api/user.js
modified: resources/js/components/global/Navigation.vue
2. git reset HEAD 把暫存區的修改返回給工作區,git status文件顯示紅色了