Fork me on Github

git奇淫技巧大全

git和svn所不同的是git是分布式的,没有服务器概念。所有的人的机器上都有一个repository,每次提交都是给自己机器的repository,一般在项目开始前我们会在本地新建一个repository,然后再将本地的repository提交到服务器;

或者现在服务器的管理页面新建一个项目然后克隆到本地,一般常见的git服务有GitHub,gitee,gitlab,gitea等;

远程的代码管理是可以基于SSH的,也可以使用http方式,要使用ssh远程的git则需要SSH的配置。

 

用ssh登录使用git

1.查看是否已经有了ssh密钥:cd ~/.ssh
如果没有密钥则不会有此文件夹,有则备份删除
2.生存密钥:

1、运行 ssh-keygen -t rsa -C "xxxxx@xxxxx.com"

xxxxx@xxxxx.com 为你自己的邮箱
按照提示完成三次回车,即可生成 ssh key。

2、cd ~/.ssh 

 有这么两个文件:id_rsa和id_rsa.pub

3.在github上添加ssh密钥,这要添加的是“id_rsa.pub”里面的公钥。

打开https://github.com/ ,登陆,然后添加ssh。


5.测试:ssh git@github.com

The authenticity of host ‘github.com (207.97.227.239)’ can’t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,207.97.227.239′ (RSA) to the list of known hosts.
ERROR: Hi tekkub! You’ve successfully authenticated, but GitHub does not provide shell access
Connection to github.com closed.

 

Git常规命令

仓库初始化(生成.git文件夹):

git init

设置用户身份信息

git config --global user.name "zhangsan"
git config --global user.email "zhangsan@gmail.com"

 

获取源码:

git clone git@github.com:FIGHTING-TOP/three.js.git

 

生成快照并存入项目索引:

git add

  

文件,还有git rm,git mv等等…
项目索引提交:

git commit

  

4.协作编程:
将本地repo于远程的origin的repo合并,
推送本地更新到远程:

git push origin master

  

更新远程更新到本地:

git pull origin master

  

添加远端repo:

git remote add upstream git://github.com/pjhyett/github-services.git

  

修改远端repo:

git remote rm origin
git remote add origin [url]

  

新建repo:

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/BrentHuang/MyRepo.git
git push -u origin master

  


强推
git push -f

  

 修改远程URL

git remote set-url origin new.git.url/here

  


repo分支操作:

git branch [branch name]
git checkout [branch name]
git push origin [branch name]
git branch -d [branch name]  // 删除
git push origin :[branch name] // 提交删除

  

git stash贮存区:

git stash save 'beizhu'
git stash list
git stash drop ''

  

 

git tag相关

 

1.创建tag:

创建 tag 是基于本地分支的 commit,而且与分支的推送是两回事,就是说分支已经推送到远程了,但是你的 tag 并没有,如果把 tag 推送到远程分支上,需要另外执行 tag 的推送命令。

git tag <tagName> //创建本地tag

git push origin <tagName> //推送到远程仓库

若存在很多未推送的本地标签,你想一次全部推送的话:

git push origin --tags

以上是基于本地当前分支的最后的一个commit 创建的 tag ,但是如果不想以最后一个,只想以某一个特定的提交为tag ,也是可以的,只要你知道commit 的id。

git log --pretty=oneline //查看当前分支的提交历史 里面包含 commit id

git tag -a <tagName> <commitId>

  

2.查看标签

查看本地某个 tag 的详细信息:

git show <tagName>

查看本地所有 tag:

git tag 或者 git tag -l

查看远程所有 tag:

git ls-remote --tags origin

  

 

3.删除标签

本地 tag 的删除:

git tag -d <tagName>

远程 tag 的删除:

git push origin :<tagName>

  

4.检出标签

git checkout -b <branchName> <tagName>

因为 tag 本身指向的就是一个 commit,所以和根据commit id 检出分支是一个道理。

但是需要特别说明的是,如果我们想要修改 tag检出代码分支,那么虽然分支中的代码改变了,但是 tag标记的 commit还是同一个,标记的代码是不会变的,这个要格外的注意。

其它

命令git tag -a <tagname> -m "XXX..." 可以指定标签信息。

命令git tag -a v0.1.0 -m "release 0.1.0 version" 创建附注标签。

命令git checkout [tagname] 切换标签。

  

 

 

 git常见问题

问题一:

github下载项目文件过大时,

git clone时会出现error: RPC failed; HTTP 504 curl 22 The requested URL returned error: 504 Gateway Time-out的问题

解决办法:
git clone --depth=1 git@github.com:FIGHTING-TOP/three.js.git 
cd three.js
git fetch --unshallow

  

问题二:

问题三:

在配置gitignore之前push了代码,删除远程保留本地

git rm -r --cached .vscode # 将文件夹从暂存区中删除
git commit -m 'delete .vscode config' # 将修改后的暂存区合并到HEAD中
git push origin master # 推到远端

 

问题四:

将想要忽略掉的文件的相关记录清理掉

1.删除暂存区文件,同时删除本地文件

git rm file_name # 删除本地文件, 同时也从暂存区中删除
git commit -m "delete" # 合并到HEAD中
git push origin master

 

 

 
posted @ 2017-03-10 11:13  王者归来!  阅读(557)  评论(0编辑  收藏  举报