您的位置:首页 > 其它

git配置和常用命令

2016-03-10 18:15 447 查看
1,在本地生成key: ssh-keygen -t rsa -C "xxx900@qq.com"

2,把生成的/home/zhuyi/.ssh/id_rsa.pub中全部内容复制, 粘贴到github网站上add key

3,测试连接:ssh -T git@github.com

4,配置本地git:

git config --global user.name "july11"

git config --global user.email "xxx900@qq.com"
 

如果为本地git库配置多用户,那么这里就不能设置global的

 要在每个git库目录下配置对应的用户:

git config user.name "july11"

5,本地git配置多用户

一个host一个key,注意第二个ssh-keygen时key file不能使用默认的名字,我设第二个为gitosc

如果是只有github托管,有多个github帐号,则可将第二个用户的设置改为:HOST 修改.ssh目录下的config, 设置多个host,这里是多用户多个托管地址,在github和gitosc分别托管项目。
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host git.oschina.net
HostName git.oschina.net
User git
IdentityFile ~/.ssh/gitosc

如果只是在github上托管,有多个帐号,则把第二个的设置改一下:HOST two.github.com

修改第二个帐号的git库配置:

it remote rm origin
git remote add origin git@two.github.com:[name2]/two.github.com.git


详细参考:
http://tmyam.github.io/blog/2014/05/07/duo-githubzhang-hu-she-zhi/ http://blog.csdn.net/itmyhome1990/article/details/42643233
6,clone一个project到本地:

git clone git@github.com:july11/july11.github.com.git
现在都推荐使用https的链接,但是如果clone https的会引起一个问题,就是每次pull或者push都需要输入用户名和密码,相当麻烦,改以下配置即可:

修改git项目根目录下的.git文件夹下的config文件:

[remote "origin"]
url = git@github.com:suyan/suyan.github.io.git
fetch = +refs/heads/*:refs/remotes/origin/*


7,提交一个版本, 远程仓库没有修改的情况

查看本地git库修改状态: git status

添加所有修改的文件: git add .

提交: git commit -m "change log"

push到远程主干: git push origin master

8, 远程仓库已修改的情况

git push时提示有冲突

先从远程库拉一份最新的: git pull

查看冲突: git status

8.1 手动解决冲突: 

打开冲突文件 HEAD<<< 到 === 之间是本地的修改, === 到 >>> 之间是远程仓库的修改

手动修改后, 去掉这些标记

8.2 用mergetool

9, 解决冲突后的提交:

合入主干(merge): git commit -a

把修改后的文件添加: git add <file>

提交本地仓库: git commit -m ""

push到远程主干: git push origin master

工具: gitk 查看status

工具: mergetool在界面中解决冲突 

10, 分支相关

查看本地当前分支: git branch

查看本地和远程所有分支: git branch -va
切换到一个远程分支: git checkout -t origin/<branch name>

11, 撤销提交

git push时提示有大文件不能上传

remote: warning: Large files detected.

remote: error: File featureExtract/ScaCtm/bow_test_Sca.txt is 173.91 MB; this exceeds Git@OSC's file size limit of 100 MB

remote: error: hook declined to update refs/heads/master

查看提交日志:git reflog

1118157 HEAD@{0}: commit: update to support folder vocabulary

6abbb56 HEAD@{1}: commit: code on bigdisk upgraded to add folder vocabulary

263473a HEAD@{2}: clone: from https://git.oschina.net/sdlucaslala/scene.git
要回退到HEAD 2:

git reset HEAD~2

再重新add, commit,push

12, 忽略文件或文件夹

在仓库根目录下  vi .gitignore

添加如下内容:

/data

/bin

保存即可

13,  本地已有项目

在git网站创建新代码库,假设地址为git@git.oschina.net:july/proj.git

进入本地项目根目录下:

cd proj

git init

git remote add origin git@git.oschina.net:july/proj.git

vim .gitignore #添加要忽略提交的目录或文件

git add src/  #添加要提交到远程仓库的文件

git commit -a -m "first commit"

git pull origin master

git push origin master
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: