一、git基本使用
1.1、创建新仓库
1 | cd target_path |
1.2、从远端仓库拉代码
以创建一个本地仓库的克隆版本
1
git clone /path/to/repository
远端服务器上的仓库
1
git clone username@host:/path/to/repository
1.3、添加与提交
1 | git add <filename> #添加某个文件 |
1 | git commit -m "代码提交信息" #提交到本地 |
你的改动现在已经在本地仓库的 HEAD 中了
1.4、添加远程仓库和推送
如果你还没有克隆现有仓库,并欲将你的仓库连接到某个远程服务器,执行如下命令:1
2git remote add origin <server>
git remote add origin git@github.com:yourname/code.git #示例
将这些改动提交到远端仓库1
git push origin master
可以把 master 换成你想要推送的任何分支
1.5、分支
分支是用来将特性开发绝缘开来的。在你创建仓库的时候,master 是“默认的”。在其他分支上进行开发,完成后再将它们合并到主分支上。
查看分支:
1
git branch
创建分支:
1
git branch <name>
切换分支:
1
git checkout <name>
创建+切换分支:
1
git checkout -b <name>
合并某分支到当前分支:
1
git merge <name>
删除分支:
1
git branch -d <name>
1.6、更新与合并
更新最新代码1
2
3
4git checkout branch_one #切换到最新的分支branch_one
git pull # 更新最新代码
git checkout branch_two #切换到最新的分支branch_two
git merge branch_one #branch_two合并branch_one
这时候就需要你修改这些文件来人肉合并这些 冲突(conflicts) 了。改完之后,你需要执行如下命令以将它们标记为合并成功1
2
3git add .
git commit -m "冲突解决"
git push origin branch_two #更新合并后的代码到远端
在合并改动之前,也可以使用如下命令查看:1
git diff <source_branch> <target_branch>
二、使用示例
创建一个新的仓库
1
2
3
4
5
6git clone git@github.com:yourname/code.git
cd code
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master从已知文件夹上传到仓库
1
2
3
4
5
6
7
8cd existing_folder
git init
git add .
git commit -m "Initial commit"
git remote add origin git@github.com:yourname/code.git
#获取远程库与本地同步合并(如果远程库不为空必须做这一步,否则后面的提交会失败)
git pull --rebase origin master
git push -u origin master已知的本地仓库
1
2
3
4cd existing_repo
git remote add origin git@github.com:xiachi/code.git
git push -u origin --all
git push -u origin --tags合并分支
1
2
3
4
5
6
7
8
9git add .
git commit -m "合并前先提交代码"
git push origin cur_branch #推送到远端
git checkout branch_one #切换分支
git pull #更新branh_one的代码
git checkout cur_branch #切换到之前的分支
git merge branch_one #合并
#人肉解决冲突
git push origin cur_branch
三、git链接多个账号
在很多情况下我们在公司使用git需要连接公司的账号,但是如果这个时候需要连接自己的github账号的时候,这个时候就需要在自己电脑上使用git链接两个账号或者多个账号
3.1、生产两个ssh-key
1 | ssh-keygen -t rsa -C "one@gmail.com" #你的github账号 |
注意:
- 不要一路回车,分别在第一个对话即需要输入的时候,输入文件名(如:id_rsa_one和id_rsa_two),这样会生成对应的文件名的公钥和私钥(如:id_rsa_one、id_rsa_one.pub、id_rsa_two、id_rsa_two.pub)
- 如果执行的路径不在~/.ssh路径中执行的话,需要把文件拷贝到期目录下
3.2、添加私钥
打开ssh-agent
如果你是github官方的bash:
1
ssh-agent -s
如果你是其它,比如msysgit:
1
eval $(ssh-agent -s)
添加私钥1
2ssh-add ~/.ssh/id_rsa_one
ssh-add ~/.ssh/id_rsa_two
3.3、创建或者修改config文件
在~/.ssh中如果没有config则使用touch config创建该文件,有的话则进行如下修改:
网上demo示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23# one(one@gmail.com)
Host one.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_one
User one
# two(two@ gmail.com)
Host two.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_two
User two自己电脑的示例(配置了gitlab和两个github账号)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Host github_one #和github的名字一致
HostName github_one.github.com
User git
IdentityFile /Users/xiachi/.ssh/id_rsa_ucas
Host mayexia #mayexia
HostName mayexia.github.com
User git
IdentityFile /Users/xiachi/.ssh/id_rsa_350
Host gitlab.com
HostName gitlab.com
User git
IdentityFile /Users/xiachi/.ssh/id_rsa
最后想连接github的话,需要将对应的公钥添加到github的ssh秘钥中,添加后便可使用git连接到github了。
四、git基本命令大全
1 | git add file_name # 向暂存区添加文件 |