@Arison
2018-08-09T06:48:51.000000Z
字数 2945
阅读 1330
A+技术栈
查看用户名和邮箱地址
git config user.namegit config user.email
验证SSH
ssh git@github.comWarning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.PTY allocation request failed on channel 0Hi Arisono! You've successfully authenticated, but GitHub does not provide shell access.Connection to github.com closed.ssh git@gitlab.com$ ssh git@gitlab.comPTY allocation request failed on channel 0Welcome to GitLab, @Arisono!Connection to gitlab.com closed.
查看配置的方法:
git config --list
查看日志[找到指定提交版本的commit_id]:
git log
回滚指定版本
git reset --hard commit_idgit reset --hard 0b9f0c8fc2bb8c04376a950a5bb90835ad9aad84
放弃本地仓库,远程仓库强制覆盖本地仓库
git fetch --allgit reset --hard origin/master//git feth 是下载远程仓库代码,不与本地仓库代码合并//git reset 把HEAD指向刚刚下载的最新版本
.gitnore文件重新生效
git rm -r --cached .git add .git commit -m 'update .gitignore'
移除版本控制
git rm -r -n --cached "bin/" ,此命令是展示要删除的文件表预览git rm -r --cached "bin/" ,删除文件的命令.git commit -m" 删除bin文件" ,提交,并加注释git push origin master ,提交到远程服务器
本地仓库初始化
git init
克隆到本地
git clone https://github.com/spring-guides/gs-gradle.git
强制推送[谨慎]:
git push -f
查看状态:
git status
添加本地:
git add .
提交本地:
git commit -m "commit at 2015-09-17"
推送到远程(远程仓库存在)
git push [remote-name] [branch-name]git push origin master [默认的提交方式]git push github master
新建远程仓库 (远程仓库不存在)
git remote add [remote-name] [url]git remote add origin url
删除远程仓库:
git remote rm [remote-name]
远程仓库下拉代码:
git pull origin master
恢复为unstaged状态:
git rm --cache test.c 【变为unstaged状态】
取消本地仓库暂时保存:
git reset test.c 【状态变为modified状态】
新建分支
git branch [branch-name] //新建分支git checkout -b [branch-name] //新建分支并切换到该分支
删除分支
git branch -d [branch-name] //删除分支
查看所有分支
git branch -v
查看本地分支:
git branch
查看所有远程仓库:
git remote -v
查看单个远程仓库:
git remote show [remote-name]
提交本地test分支作为远程的master分支:
git push origin test:master
提交本地test分支作为远程的test分支:
git push origin test:test
在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 "文件路径",不仅将该文件从缓存中删除,还会将物理文件删除(不会回收到垃圾桶)。