Git使用的一些问题:.gitignore规则不生效、git同步代码至github和gitee
<h1 id="git忽略规则及gitignore规则不生效的解决办法">Git忽略规则及.gitignore规则不生效的解决办法</h1><h2 id="gitignore-的基本使用">.gitignore 的基本使用</h2>
<p>在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件)。这个文件每一行保存了一个匹配的规则。</p>
<pre><code># 此为注释 – 将被 Git 忽略
*.a # 忽略所有 .a 结尾的文件
!lib.a # 但 lib.a 除外
/TODO# 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
</code></pre>
<p>规则很简单,不做过多解释,idea 里有个 .ignore 插件,还是挺好用的</p>
<h2 id="gitignore-规则不生效">.gitignore 规则不生效</h2>
<p>有时候在项目开发过程中,突然心血来潮想把某些目录或文件加入忽略规则,按照上述方法定义后发现并未生效!</p>
<p>原因是: <strong>.gitignore 只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的</strong>。</p>
<p>解决方法:先把本地缓存删除(改变成未track状态),然后再提交:</p>
<pre><code>git rm -r --cached .
git add .
git commit -m 'update .gitignore
</code></pre>
<h1 id="git同步代码至github和gitee码云">git同步代码至github和gitee(码云)</h1>
<p>我们有时候开发代码需要把代码同步到多个远程库中,如何操作才能做到呢?</p>
<p>我们知道,git 是分布式版本控制系统,同步到多个远程库时,需要用不同的名称来标识不同的远程库,而 git 给远程库起的默认名称是 origin。所以我们需要修改、配置名称,以关联不同远程库。有两种方式!</p>
<p>为了方便举例,我以GitHub和Gitee(码云)作为示例!</p>
<h2 id="命令方式同步">命令方式同步</h2>
<p>先删除已关联的名为 origin 的远程库:</p>
<pre><code>git remote rm origin
</code></pre>
<p>然后,再关联 GitHub 的远程库:</p>
<pre><code>git remote add github git@github.com:chloneda/demo.git
</code></pre>
<p>接着,再关联 Gitee 的远程库:</p>
<pre><code>git remote add gitee git@gitee.com:chloneda/demo.git
</code></pre>
<h2 id="配置方式同步">配置方式同步</h2>
<p>修改 .git 文件夹内的 config 文件:</p>
<pre><code>
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
url = git@github.com:chloneda/demo.git
fetch = +refs/heads/*:refs/remotes/github/*
remote = origin
merge = refs/heads/master
</code></pre>
<p>将上述文件内容 内容复制,修改 origin 名称,内容如下:</p>
<pre><code>
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
url = git@github.com:chloneda/demo.git
fetch = +refs/heads/*:refs/remotes/github/*
url = git@gitee.com:chloneda/demo.git
fetch = +refs/heads/*:refs/remotes/gitee/*
remote = origin
merge = refs/heads/master
</code></pre>
<h2 id="查看远程库">查看远程库</h2>
<p>通过以上两种方式的任一种方式配置完成后,我们用 <code>git remote -v</code> 查看远程库信息:</p>
<pre><code>gitee git@gitee.com:chloneda/demo.git (fetch)
gitee git@gitee.com:chloneda/demo.git (push)
githubgit@github.com:chloneda/demo.git (fetch)
githubgit@github.com:chloneda/demo.git (push)
</code></pre>
<p>可以看到两个远程库,说明配置生效了。</p>
<h2 id="上传代码">上传代码</h2>
<pre><code>git add .
git commit -m "update"
</code></pre>
<h2 id="提交到github">提交到github</h2>
<pre><code>git push github master
</code></pre>
<h2 id="提交到码云">提交到码云</h2>
<pre><code class="language-shell">git push gitee master
</code></pre>
<h2 id="更新代码">更新代码</h2>
<pre><code># 从github拉取更新
git pull github
# 从gitee拉取更新
git pull gitee
</code></pre>
<h2 id="踩到的坑">踩到的坑</h2>
<p>上述过程中,更新或提交代码时可能会遇到 <strong>fatal:refusing to merge unrelated histories</strong>(拒绝合并无关的历史) 错误,解决办法:</p>
<p>首先将远程仓库和本地仓库关联起来。</p>
<pre><code>git branch --set-upstream-to=origin/remote_branchyour_branch
</code></pre>
<p>其中,origin/remote_branch是你本地分支对应的远程分支,your_branch是你当前的本地分支。</p>
<p>然后使用git pull整合远程仓库和本地仓库。</p>
<pre><code>git pull --allow-unrelated-histories # 忽略版本不同造成的影响
</code></pre>
<p>重新更新、提交即可。</p>
<p>注: 如遇到 <strong>Git没有共同祖先的两个分支合并</strong> 的情形请自行查询!</p>
<hr>
<p>本文转载自:</p>
<p>https://blog.csdn.net/yingpaixiaochuan/article/details/53729446</p>
<p>https://zhuanlan.zhihu.com/p/71163868</p><br><br>
来源:https://www.cnblogs.com/songjilong/p/12627355.html
頁:
[1]