[关闭]
@Arison 2018-08-09T06:48:51.000000Z 字数 2945 阅读 1135

Git知识点

A+技术栈


配置

查看用户名和邮箱地址

  1. git config user.name
  2. git config user.email

验证SSH

  1. ssh git@github.com
  2. Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
  3. PTY allocation request failed on channel 0
  4. Hi Arisono! You've successfully authenticated, but GitHub does not provide shell access.
  5. Connection to github.com closed.
  6. ssh git@gitlab.com
  7. $ ssh git@gitlab.com
  8. PTY allocation request failed on channel 0
  9. Welcome to GitLab, @Arisono!
  10. Connection to gitlab.com closed.

查看配置的方法:

  1. git config --list

日志与版本管理

查看日志[找到指定提交版本的commit_id]:

  1. git log

回滚指定版本

  1. git reset --hard commit_id
  2. git reset --hard 0b9f0c8fc2bb8c04376a950a5bb90835ad9aad84

放弃本地仓库,远程仓库强制覆盖本地仓库

  1. git fetch --all
  2. git reset --hard origin/master
  3. //git feth 是下载远程仓库代码,不与本地仓库代码合并
  4. //git reset 把HEAD指向刚刚下载的最新版本

.gitnore文件重新生效

  1. git rm -r --cached .
  2. git add .
  3. git commit -m 'update .gitignore'

移除版本控制

  1. git rm -r -n --cached "bin/" ,此命令是展示要删除的文件表预览
  2. git rm -r --cached "bin/" ,删除文件的命令.
  3. git commit -m" 删除bin文件" ,提交,并加注释
  4. git push origin master    ,提交到远程服务器

基础命令

本地仓库初始化

  1. git init

克隆到本地

  1. git clone https://github.com/spring-guides/gs-gradle.git

强制推送[谨慎]:

  1. git push -f

查看状态:

  1. git status

添加本地:

  1. git add .

提交本地:

  1. git commit -m "commit at 2015-09-17"

推送到远程(远程仓库存在)

  1. git push [remote-name] [branch-name]
  2. git push origin master [默认的提交方式]
  3. git push github master

新建远程仓库 (远程仓库不存在)

  1. git remote add [remote-name] [url]
  2. git remote add origin url

删除远程仓库:

  1. git remote rm [remote-name]

远程仓库下拉代码:

  1. git pull origin master

状态

恢复为unstaged状态:

  1. git rm --cache test.c 【变为unstaged状态】

取消本地仓库暂时保存:

  1. git reset test.c 【状态变为modified状态】

分支

新建分支

  1. git branch [branch-name] //新建分支
  2. git checkout -b [branch-name] //新建分支并切换到该分支

删除分支

  1. git branch -d [branch-name] //删除分支

查看所有分支

  1. git branch -v

查看本地分支:

  1. git branch

查看所有远程仓库:

  1. git remote -v

查看单个远程仓库:

  1. git remote show [remote-name]

提交本地test分支作为远程的master分支:

  1. git push origin test:master

提交本地test分支作为远程的test分支:

  1. git push origin test:test

.gitignore

在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件)。这个文件每一行保存了一个匹配的规则例如:

说明
# 此为注释 将被 Git 忽略
*.a 忽略所有 .a 结尾的文件
!lib.a 但 lib.a 除外
/TODO 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/ 忽略 build/ 目录下的所有文件
doc/*.txt 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt

Git忽略规则及.gitignore规则不生效的解决办法
.gitignore无效,不能过滤某些文件
利用.gitignore过滤文件,如编译过程中的中间文件,等等,这些文件不需要被追踪管理。
现象:a
在.gitignore添加file1文件,以过滤该文件,但是通过git status查看仍显示file1文件的状态。a
原因
在git库中已存在了这个文件,之前push提交过该文件。
.gitignore文件只对还没有加入版本管理的文件起作用,如果之前已经用git把这些文件纳入了版本库,就不起作用了
解决
需要在git库中删除该文件,并更新。
然后再次git status查看状态,file1文件不再显示状态。

规则很简单,不做过多解释,但是有时候在项目开发过程中,突然心血来潮想把某些目录或文件加入忽略规则,按照上述方法定义后发现并未生效,原因是.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。那么解决方法就是先把本地缓存删除(改变成未track状态),然后再提交:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

使用 git rm 命令即可,有两种选择,
一种是 git rm --cached "文件路径",不删除物理文件,仅将该文件从缓存中删除;
一种是 git rm --f "文件路径",不仅将该文件从缓存中删除,还会将物理文件删除(不会回收到垃圾桶)。

总结


参考文献:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注