Git 分支管理与本地 repository 创建
如何选择开源许可证?(转载)

GIT 备忘

tubo posted @ 2014年9月03日 00:09 in 未分类 , 815 阅读

GitMemo

Author: Tubo

近来一直在用 git 来管理代码,使用过程中会时不时地遇到一些问题或者有些想法,然后就去查该怎么做。 这里将查到的一些东西做个记录,以便查阅。

这不是一个 git 的 FAQ, 但如果有兴趣或者有需要,可以来 这里 查阅 GitFaq。

此外,如果没有接触过 Git, 推荐阅读一下 OpenGit 上的 progit

1 分支管理

 

1.1 怎样在本地创建一个分支?

很简单:

git branch BranchName

但上述命令仅仅创建一个 branch,并不会自动切换到新的分支,想要切换的话需要使用 git checkout:

git checkout BranchName

或者可以在 checkout 后面加上 "-b" 选项,形如:

git checkout -b BranchName

1.2 怎样创建一个远端的分支?

无需单独创建,直接 push 即可,形如:

git push origin BranchName

1.3 怎样删除本地分支?

git branch -d BranchName

如果我们对名为 BranchName 的分支做了一些更改,但在尚未合并到其他分支的时候就想直接将其删掉, 可以利用 -D 选项:

git branch -D BranchName

1.4 怎样删除远端分支?

很多 git 的命令都可以通过 "-r" 来表明要对远端进行操作,但

git branch -d -r BranchName

却不会删除远端的分支 —— 它仅仅告诉 git 不用追踪远端分支的变化。

删除远端分支优点 tricky:

git push origin :BranchName

2 Diff 相关

 

2.1 如何查看某次提交更改了哪些文件?

git diff --name-only SHA1 SHA2

其中, SHA1 SHA2 为两次 commit 的 SHA1 值。

2.2 如何打补丁

使用 git apply , 具体可参考 progit 中相关内容。

 

3 Miscs

 

3.1 如何找到 GIT repository 的根目录?

git rev-parse --show-toplevel

3.2 GIT Submodule 是什么,怎么用?

中文叫做“子模块”,用来将外部的 repository 嵌入到本地的代码目录。下面的链接很不错,值得阅读: http://chrisjean.com/2009/04/20/git-submodules-adding-using-removing-and-updating/

3.3 core.autocrlf

(http://www.open-open.com/lib/view/open1328070404827.html)

假如你正在Windows上写程序,又或者你正在和其他人合作,他们在Windows上编程,而你却在其他系统上,在这些情况下,你可能会遇到行尾结束符问题。这是因为Windows使用回车和换行两个字符来结束一行,而Mac和Linux只使用换行一个字符。虽然这是小问题,但它会极大地扰乱跨平台协作。

Git可以在你提交时自动地把行结束符CRLF转换成LF,而在签出代码时把LF转换成CRLF。用core.autocrlf来打开此项功能,如果是在Windows系统上,把它设置成true,这样当签出代码时,LF会被转换成CRLF:

$ git config --global core.autocrlf true

Linux或Mac系统使用LF作为行结束符,因此你不想 Git 在签出文件时进行自动的转换;当一个以CRLF为行结束符的文件不小心被引入时你肯定想进行修正,把core.autocrlf设置成input来告诉 Git 在提交时把CRLF转换成LF,签出时不转换:

$ git config --global core.autocrlf input

这样会在Windows系统上的签出文件中保留CRLF,会在Mac和Linux系统上,包括仓库中保留LF。

如果你是Windows程序员,且正在开发仅运行在Windows上的项目,可以设置false取消此功能,把回车符记录在库中:

$ git config --global core.autocrlf false



3.4 GIT: ignoring changes in tracked files (http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/)

There may be times when you want to edit some variables in for example a database connection file, to run an application right from within your GIT repo. Of course you don’t wont those changes to be commited, so you add the file the .gitignore.
However adding tracked files to .gitignore won’t work because GIT will still track the changes and commit the file if you use the -a parameter.

Fortunately GIT has a very easy solution for this, just run the following command on the file or path you want to ignore the changes of:

git update-index --assume-unchanged <file>

If you wanna start tracking changes again run the following command:

git update-index --no-assume-unchanged <file>

You can find more info about this in the git manual.

 

3.5 How do I remove a git submodule?

To remove a submodule you need to:

  1. Delete the relevant section from the .gitmodules file.
  2. Delete the relevant section from .git/config.
  3. Run git rm --cached path_to_submodule (no trailing slash).
  4. Commit and delete the now untracked submodule files.
http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule
https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial
 

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter