git远程操作入门
一、建立本地库
新建文件夹 -> 改变目录 -> 初始化
~/Desktop
$ mkdir learngit
~/Desktop
$ cd learngit
~/Desktop/learngit
$ git init
Initialized empty Git repository in C:/.../Desktop/learngit/.git/
新建的文件夹是空的,所以是empty Git repository。
二、在本地库中添加内容
添加文件 -> add -> commit
1、先随便在learngit内放入一些文件,如readme.md,master表示当前分支。
~/Desktop/learngit (master)
$ ls
learngit.txt
2、利用git status 查看当前状态
~/Desktop/learngit (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
learngit.txt
nothing added to commit but untracked files present (use "git add" to track)
3、将readme.md放入暂存区
~/Desktop/learngit (master)
$ git add learngit.txt
4、查看当前状态
~/Desktop/learngit (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: learngit.txt
5、上传到本地库
~/Desktop/learngit (master)
$ git commit -m "learn git"
[master (root-commit) 3618929] upload readme
1 file changed, 3 insertions(+)
create mode 100644 readme.md
6、查看当前状态
~/Desktop/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean
三、连接远程库
添加公钥 -> 连接远程库 -> 上传
1、第一次使用远程库连接git或者coding需要配置git和公钥。
设置完公钥(.pub),将公钥内容复制到git或者coding的相关地方即可。
$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"
$ ssh-keygen -t rsa
$ cat id_rsa.pub
2、远程连接
origin为自取的名字,可换成其他名称,add后可加https和ssh地址。只有连接已经设置公钥的远程库才能进行操作,连接别人的远程库无法操作。
~/Desktop/learngit (master)
$ git remote add origin git@git.coding.net:username/project name.git
~/Desktop/learngit (master)
$ git remote
origin
git remote查看当前远程库
3、上传
利用push将通过add和commit语句将文件上传至本地库。
~/Desktop/learngit (master)
$ git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 307 bytes | 307.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@git.coding.net:username/project name.git
4d093df..820f725 master -> master
四、一些常用语句
本地库初始化:git init
上传到缓存区:git add xxx
提交到本地库:git commit -m “xxxx”
查看分支:git branch
创建分支:git branch
切换分支:git checkout
创建+切换分支:git checkout -b
合并某分支到当前分支:git merge
删除分支:git branch -d
连接远程库:git remote add origin
删除连接:git remote rm origin
查看当前远程库:git remote
上传到远程库:git push origin master
下载到本地库:git pull origin master
克隆到本地库:git clone