琐碎记录 琐碎记录

Mint安装Git

2013-01-05

添加ppa源

如git的ppa源:
sudo add-apt-repository ppa:git-core/ppa

更新软件列表

sudo apt-get update

安装软件

如安装git:
> sudo apt-get install git

生成/添加SSH公钥:
> ssh-keygen -t rsa -C "test@test.com"
> cat ~/.ssh/id_rsa.pub
复制生成后的 ssh key 添加即可

使用命令

#查看当前目录git配置
git config -l

#配置当前git仓库用户和邮箱
git config user.name yourname
git config user.email yourname@gmail.com

# 更改git仓库地址
git remote set-url origin https://github.com/yourgithub/yourproject.git
或
git remote set-url origin ssh://git@git.yourdomain.com:10022/you/test.git

#保存密码下次git push的时候不需要再输入
git config --global credential.helper store

#创始化及发布
git init
git remote add origin https://github.com/yourgithub/yourproject.git
git add .
git commit -m "init project"
git push origin master

#创建分支
git branch your_project_branch

#查看分支
git remote show origin
git branch -lva

#切换本地已经拉下来的分支
git checkout master
git checkout dev

#切换本地未拉下来的分支并且远程存在的分支
git checkout -t origin/master
git checkout -t origin/dev

#切换本地未拉下来的分支并且远程也不存在的分支
git checkout -b origin/master
git checkout -b origin/dev

# 重新设置当前分支起点到某个分支
git branch -lva
git rebase origin/master

# test分支重新恢复到其它分支
git reset --hard origin/master
or
git reset --hard 19fe787
本地删除remote下test分支,远程仓库也删除test分支
本地提交test即可

#克隆clone
git clone https://github.com/yourgithub/yourproject.git -b your_project_branch
git clone git@github.com/yourgithub/yourproject.git
git clone ssh://git@github.com:3333/yourgithub/yourproject.git
ssh部署代码需要设置deploy key xxxx/yourgithub/yourproject/settings/repository Deploy Keys <- cat ~/.ssh/id_rsa.pub
非22端口则需要设置:
> vim ~/.ssh/config:
host github.com
port 3333

#同一个工程push多个远程仓库,url第一条为主仓库地址,其它作备份仓库:
> vim /test/.git/config
[remote "origin"]
	url = ssh://git@gitlab.com:10022/my/test.git
    url = git@gitee.com:my/test.git
    url = git@bitbucket.org:my/test.git

#查看日志
git log

#查看所有文件改动详情(根据日志commit ID)
git show fw8q0fqef0q9ww09weg8wq678

#查看某个文件改动详情(根据日志commit ID,当前目录下的java文件地址)
git show fw8q0fqef0q9ww09weg8wq678 src/main/java/com/google/demo/MyTest.java
#或
cd src/main/java/com/google/demo
git show fw8q0fqef0q9ww09weg8wq678 MyTest.java

#查看所有文件改动
git whatchanged

git多ssh列表

(ecs还需配合远程机器.ssh/authorized_keys里追加本地id_rsa_xx.pub文本内容)
> ssh-keygen -t rsa -C "ecs0@aliyun.com" #提示rsa存放路径时id_rsa_ecs0
> ssh-keygen -t rsa -C "ecs1@aliyun.com" #提示rsa存放路径时id_rsa_ecs1
> ssh-keygen -t rsa -C "user1@github.com" #同上
> ssh-keygen -t rsa -C "user2@github.com" #同上
> vim ~/.ssh/config

#ecs0-prod-web
Host xx.xx.xx.xx
	port 22
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/ecs/id_rsa_ecs0

#ecs1-test-web
Host xx.xx.xx.xx
	port 2233
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/ecs/id_rsa_ecs1

#coding
Host e.coding.net
	HostName e.coding.net
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/coding/id_rsa_coding

#github-user1
Host user1.github.com
	HostName github.com
	User user1
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/github/id_rsa_github_u1

#github-user2
Host user2.github.com
	HostName github.com
	User user2
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/github/id_rsa_github_u2

git问题列表

排查问题debug:
> ssh -T git@e.coding.net  (校验)
> ssh git@e.coding.net -vT (debug模式)
> ssh -T -vvv git@e.coding.net (debug详细模式)

问题:cmd命令中ssh模式git pull正常但eclipse中始终错误
解决:
1.mac下另外新建启动脚本~/bin/eclipse.sh:
#!/bin/sh
export GIT_SSH="/usr/bin/ssh"
open -n /Applications/Eclipse.app
2.eclipse->General->Network...->SSH2设置:
SSH2 HOME: /Users/xxx/.ssh(指向.ssh根目录,多账户时必须为config文件所在目录,默认~/.ssh)
Private keys: id_rsa(私钥文件名)
3.使用~/bin/eclipse.sh脚本重启Eclipse即可

问题:debug下SSH连接无响应失败
解决:有可能ssh_config没有开启HostKeyAlgorithms ssh-rsa,ssh-dss
> vim /etc/ssh/ssh_config
Host *
        GSSAPIAuthentication yes
# If this option is set to yes then remote X11 clients will have full access
# to the original X11 display. As virtually no X11 client supports the untrusted
# mode correctly we set this to yes.
        ForwardX11Trusted yes
# Send locale-related environment variables
        SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
        SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
        SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
        SendEnv XMODIFIERS
        HostKeyAlgorithms ssh-rsa,ssh-dss


问题:Warning: Permanently added the RSA host key for IP address 'xx.xx.x.x' to the list of known hosts.
解决:chmod 700 ~/.ssh/id_rsa
> vim /etc/hosts
192.30.253.112  github.com
18.205.93.1     bitbucket.org
> /etc/init.d/networking restart

问题:Load key "~/.ssh/id_rsa": invalid format
解决:这是私钥的问题,一般不能手动编辑,可能多删除了最后一行回车键,如果不是则只能重新生成

问题:load pubkey "~/.ssh/id_rsa": invalid format
解决:这是公钥的问题,直接根据私钥重新生成一次:ssh-keygen -f ~/.ssh/id_rsa -y > ~/.ssh/id_rsa.pub

问题:github多个帐户公用同一台电脑ssh公钥冲突
解决:由于github限制公钥只能用在一个账户配置上,可以生成多个公钥配合用户名加以区分
> vim ~/.ssh/config:
#用户1 github
Host myname0.github.com
	HostName github.com
	User myname0
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/id_rsa
#用户2 github
Host myname1.github.com
	HostName github.com
	port 22
	User myname1
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/id_rsa1
	
Host xx.xx.ecs.ip
	port 22
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/by/esc/id_rsa
	
> vim ./myname0demo-project/.git/config
[remote "origin"]
	url = git@myname0.github.com:myname0/demo.git
> vim ./myname1demo-project/.git/config
[remote "origin"]
	url = git@myname1.github.com:myname1/demo.git


上一篇 FTP相关

Content