git 使用 gitignore

git 使用

  1. git简介

  2. git 命令

  3. git  忽略文件

git   作用:版本控制

Git 是一个开源的分布式版本控制软件,用以有效、高速的处理从很小到非常大的项目版本管理。 Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发。Git 是根据GNU通用公共许可证版本2的条款分发的自由/免费软件,安装参见:http://git-scm.com/

GitHub是一个基于Git的远程文件托管平台(同GitCafe、BitBucket和GitLab等)。

Git本身完全可以做到版本控制,但其所有内容以及版本记录只能保存在本机,如果想要将文件内容以及版本记录同时保存在远程,则需要结合GitHub来使用。使用场景:

  • 无GitHub:在本地 .git 文件夹内维护历时文件
  • 有GitHub:在本地 .git 文件夹内维护历时文件,同时也将历时文件托管在远程仓库

目前已使用Git的四个命令,这四个命令已经可以代替本地多个文件保存版本的方式:

下图盗自阮一峰博客

日常要记住下面6个命令

 git 几个专有名词

  • Workspace:工作区
  • Index / Stage:暂存区
  • Repository:仓库区(或本地仓库)
  • Remote:远程仓库
# 在当前目录新建一个Git代码库
$ git init   #已掌握

# 新建一个目录,将其初始化为Git代码库
$ git init [project-name]    

# 下载一个项目和它的整个代码历史
$ git clone [url]   #已掌握
  • git init,初始化,表示即将对当前文件夹进行版本控制。新建一个本地仓库。
  • git status,查看Git当前状态,如:那些文件被修改过、那些文件还未提交到版本库等。
  • git add 文件名,将指定文件提交到暂存区。
  • git commit -m '提交信息',将暂存区的文件提交到版本库的分支。
  • git log,查看提交记录,即:历史版本记录

branch   命令

branch相关常用命令:

  • git branch 分支名称             创建分支
  • git checkout 分支名称          切换分支
  • git branch -m 分支名称        创建并切换到指定分支
  • git branch                          查看所有分支
  • git branch -d 分支名称         删除分支
  • git merge 分支名称              将指定分支合并到当前分支

想要回到某个历史版本,使用git log   

黄色的字符串是这个版本的标识符

使用

git reset --hard 黄色的随机字符串

如果我回到了初次修改的状态吗,再想回到第二次修改的状态

,可以使用git reflog 

stash

git stash   将工作区修改的内容存储到另一个地方

git stash pop    将另一个地方在取到拿回来放到原来的代码的地方

合并的时候有时候可能会出现冲突

手动删除修改


git忽略文件

 A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected;

see the NOTES below for details.

gitignore文件指定Git应该忽略的有意未经跟踪的文件。 Git已经跟踪的文件不受影响; 有关详细信息,请参阅下面的注释。

gitignore 文件的每一行都指定了一个pattern

pattern format

  • 空白行不会指定任何文件,可作为可读性的分割。
  • 以# 开头的行作为注释
  • 每行的结尾空白会被忽略,除非引用了
  • 每行以! 开头表示取反
  • 如果pattern 以 slash(/)  结尾,则它会被移除。但是它将只会匹配目录。换句话说 foo/  会匹配foo 目录,和下面的路径
  • 如果一个pattern 不包含 slash(/) ,git 会将它作为全局的pattern 对待。
  • 如果pattern 有/ ,则* 不会匹配这个/ 字符,
  • Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag:wildcards in the pattern will not match a / in the pathname.通配符不会匹配路径里的slash(/)           For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

  • A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:

  • A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".

  • A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.

  • A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

  • Other consecutive asterisks are considered invalid.

在git工作区的根目录下,新建.gitignore 文件,吧忽略的文件名添加进去,git 就能忽略那些文件名了。

在gitbash 输入 vim .gitignore文件    键入文件名  

保存,

note:  .gitignore 要提交到git 上。  检验.gitignore标准是git status命令

在github 上别人写的git 文件,可以使用

链接 https://github.com/github/gitignore

git 忽略文件规则 http://ybin.cc/git/gitignore-syntax/ 

git 多人协作开发

GitHub 多人协作开发 三种方式:
一、Fork 方式 网上介绍比较多的方式(比较大型的开源项目,比如cocos2d-x)
开发者 fork 自己生成一个独立的分支,跟主分支完全独立,pull代码后,项目维护者可根据代码质量决定是否merge代码
此方式网上方法比较多,这里不详细描述
 
有两种常用的方法在GitHub上建立团队合作:
 
二、组织 – 组织的所有者可以针对不同的代码仓库建立不同访问权限的团队。
Accounts Settings => Organizations =>Create new Organizations 新建一个组织 然后添加项目成员,根据提示设置完毕即可。
新建一个Repository 新建完毕后 进入Repository的Settings =>Collaborators 在Teams下面点击刚创建的组织 比如eveloper-51/owners
里面就可以添加或者remove组织成员
 
三、合作者 – 代码仓库的所有者可以为单个仓库增加具备只读或者读写权限的协作者。
合作者方式比较实用,也很方便,新建一个Repository,完毕之后,进入Repository的Settings,然后在Manage Collaborators里就可以管理合作者了。
其他合作者,实用 ssh-keygen -C "YourEmail@example.com" (这里的email使用github账号)生成公钥和私钥,在Accounts Settings=》SSH keys 将公钥上传上去。
上传完成后,可使用Tower(Mac下Git管理工具)clone remote Repository 使用SSH方式登录(这里的私钥使用刚才生成的) 这样,其他合作者就可以正常的PUSH代码了。

 

 

 

原文地址:https://www.cnblogs.com/yuyang26/p/7544821.html