摘要
目标是看了这边文章能够满足最基本的git使用需求。整个文章的远程git库以git.osc为例。
初始化
安装好后,鼠标右键点git bash here,会出来命令行界面。
首先到~目录
root@host MINGW64 /d/
$ cd
root@host MINGW64 ~
$
用户名和邮箱
必须有,远程git库通过这个确定是谁提交的代码。
git config --global user.name "username"
git config --global user.email "email"
设置SSH
项目链接分为 https 或者 ssh
- 如果是https,每次提交代码到远程git库需要输入osc的用户名和密码,比较麻烦。
- 如果是ssh,需要生成一份rsa密钥,把私钥保存在本地~/.ssh目录,公钥存在osc账号中。这样每次push,pull代码不需要输入密码,非常方便。
ssh生成密钥
ssh-keygen -t rsa
,按三个回车,密码为空。
看看生成结果。
root@host MINGW64 ~
$ cd .ssh/
root@host MINGW64 ~/.ssh
$ ls
id_rsa id_rsa.pub known_hosts
root@host MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa XXX.....XXX levonhe@163.com
osc中添加公钥
路径是 个人资料 -> ssh公钥
代码维护
创建本地代码库
root@host MINGW64 /d
$ mkdir projectA
root@host MINGW64 /d
$ cd projectA/
root@host MINGW64 /d/projectA
$ git init
Initialized empty Git repository in D:/projectA/.git/
关联远程代码库
root@host MINGW64 /d/projectA (master)
$ git remote add oschina git@git.oschina.net:levonhe/JiSenMall.git
root@host MINGW64 /d/projectA (master)
$ git remote -v
oschina git@git.oschina.net:root/projectA.git (fetch)
oschina git@git.oschina.net:root/projectA.git (push)
从远程库下载代码
root@host MINGW64 /d/projectA (master)
$ git pull oschina
remote: Counting objects: 8268, done.
remote: Compressing objects: 100% (5808/5808), done.
remote: Total 8268 (delta 2480), reused 7970 (delta 2284)
Receiving objects: 100% (8268/8268), 123.92 MiB | 586.00 KiB/s, done.
Resolving deltas: 100% (2480/2480), done.
From git.oschina.net:levonhe/projectA
* [new branch] branch1 -> oschina/branch1
* [new branch] master -> oschina/master
* [new tag] v0.1.1 -> v0.1.1
You asked to pull from the remote 'oschina', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.
提交代码流程
git add .
->git commit
->git push oschina $branch
branch是分支名字。如果代码有冲突,
git fetch
->git merge
-> 修改冲突文件 ->git add .
->git commit
->git push