1. 程式人生 > >git正確的刪除遠端倉庫的檔案並用.gitignore忽略提交此檔案

git正確的刪除遠端倉庫的檔案並用.gitignore忽略提交此檔案

我向遠端倉庫提交了如下檔案src/ pom.xml target/ WebContent/,發現沒必要提交target目錄。 
於是做了如下操作:

<code class="hljs sql has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">git rm -r <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--cached target</span>
git <span class="hljs-operator" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">commit</span> -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"delete target/"</span>
git push origin master</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

開啟github看一下,target目錄是不是沒有提交了! 
如果想把target目錄以後都不用提交,可以作如下

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"> vi  <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gitignore</span>  
 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#寫入:</span>
 target/
 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#可以在.gitignore中寫入我們不想提交的檔案或目錄</span>
 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#儲存退出</span>
 git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">add</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gitignore</span>
 git commit -m <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"write .gitignore"</span>
 git <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">push</span> origin master</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

以後就不會提交target目錄了!