github创建远程仓库和git常用命令

git创建远程仓库

  首先到github页面上创建仓库(repository)如下:

wKioL1mb432DJZiwAACSCVaoQzI971.png-wh_50

  然后初始化文件夹为仓库,并提交到远程仓库,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@hxy aa]# git init
Initialized empty Git repository in /data/mydata/aa/.git/
[root@hxy aa]# git add .
[root@hxy aa]# git commit -m "first commit"
[master (root-commit) 606ee64] first commit
 2 files changed, 293 insertions(+), 0 deletions(-)
 create mode 100644 README
 create mode 100644 README.md
[root@hxy aa]# git remote add origin [email protected]:mghxy123/test.git
[root@hxy aa]# git push -u origin master
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
[root@hxy aa]# git push -u origin master
Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 1.63 KiB, done.
Total 4 (delta 0), reused 0 (delta 0)
To [email protected]:mghxy123/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[root@hxy aa]#

    我这里报错的原因是因为没有把公钥放到github里面导致的,报错后我把公钥放到了github里面就没问题了.


  或者通过克隆的方式把库拉下来,然后再提交如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@hxy aa]# git clone [email protected]:mghxy123/test1.git
Initialized empty Git repository in /data/mydata/aa/test1/.git/
warning: You appear to have cloned an empty repository.
[root@hxy aa]# ls -al
 28
drwxr-xr-x 3 root root 4096 8  22 16:10 .
drwxr-xr-x 5 root root 4096 8  22 14:56 ..
-rw-r--r-- 1 root root 9657 8  22 14:57 README
-rw-r--r-- 1 root root    7 8  22 15:55 README.md
drwxr-xr-x 3 root root 4096 8  22 16:10 test1
[root@hxy aa]# cd test1/
[root@hxy test1]# ls -al
 12
drwxr-xr-x 3 root root 4096 8  22 16:10 .
drwxr-xr-x 3 root root 4096 8  22 16:10 ..
drwxr-xr-x 7 root root 4096 8  22 16:10 .git
[root@hxy test1]# cp ../README ./
[root@hxy test1]# ls
README
[root@hxy test1]# ls
README
[root@hxy test1]# git add .
[root@hxy test1]# git commit -m "add README"
[master (root-commit) d57f411] add README
 1 files changed, 292 insertions(+), 0 deletions(-)
 create mode 100644 README
[root@hxy test1]# git  push
No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.
error: failed to push some refs to '[email protected]:mghxy123/test1.git'
[root@hxy test1]# git  push origin master
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 1.60 KiB, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:mghxy123/test1.git
 * [new branch]      master -> master

        我这里的报错是因为首次提交需要 git commit -m "add README"

        不然就是我这样的报错

我常用的一些git命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
git简写
git st   # git status
git ci   # git commit
git br   # git branch
git co   # git checkout
git mg   # git merge
git line # git log --oneline
 
git init          #初始化gi库
git add file             #增加库文件,也可以用git add .意思为添加所有
git commit -m "update"    #提交并注释
git push origin -u master master #首次推送到远程仓库
git push origin test        #推送到test分支
git branch           #查看本地分支
git branch -a            #查看所有分支
git branch test      #创建test分支,但不切换
git checkout -b test        #如果test分支存在就切换到test分支,如果不存在就创建并且换到test分支
git checkout test               #切换到test分支
git push          #推送
git push -u origin/test         #提交本地库到远程test分支
git push origin test:test   #提交本test分支作为test分支
git push origin test:master     #提交本test分支作为master分支
git branch -d test      #删除本地分支
git push origin --delete crond     #删除远程分支
git push origin :test       #删除远程分支
git branch -m | -M oldbranch newbranch #重命名分支,如果newbranch名字分支已经存在,则需要使用-M强制重命名,否则,使用-m进行重命名
git branch -d | -D test   #删除本地test分支
git branch -d -r test      #删除远程分支
git remote -v                           #查看远程仓库
git remote add [name] [url]             #添加远程仓库
git remote rm [name]                    #删除远程仓库
git remote set-url --push[name][newUrl] #修改远程仓库


下面是网上找的git常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
查看、添加、提交、删除、找回,重置修改文件
git help <command# 显示command的help
git show # 显示某次提交的内容 git show $id
git co -- <file# 抛弃工作区修改
git co . # 抛弃工作区修改
git add <file# 将工作文件修改提交到本地暂存区
git add . # 将所有修改过的工作文件提交暂存区
git rm <file# 从版本库中删除文件
git rm <file> --cached # 从版本库中删除文件,但不删除文件
git reset <file# 从暂存区恢复到工作文件
git reset -- . # 从暂存区恢复到工作文件
git reset --hard # 恢复最近一次提交过的状态,即放弃上次提交后的所有本次修改
git ci <file
git ci . 
git ci -a # 将git add, git rm和git ci等操作都合并在一起做
git ci -am "some comments"
git ci --amend # 修改最后一次提交记录
git revert <$id# 恢复某次提交的状态,恢复动作本身也创建次提交对象
git revert HEAD # 恢复最后一次提交的状态
查看文件diff
git diff <file# 比较当前文件和暂存区文件差异 git diff
git diff <id1><id2> # 比较两次提交之间的差异
git diff <branch1>..<branch2> # 在两个分支之间比较
git diff --staged # 比较暂存区和版本库差异
git diff --cached # 比较暂存区和版本库差异
git diff --stat # 仅仅比较统计信息
查看提交记录
git log git log <file# 查看该文件每次提交记录
git log -p <file# 查看每次详细修改内容的diff
git log -p -2 # 查看最近两次详细修改内容的diff
git log --stat #查看提交统计信息
Git 本地分支管理
查看、切换、创建和删除分支
git br -r # 查看远程分支
git br <new_branch> # 创建新的分支
git br -v # 查看各个分支最后提交信息
git br --merged # 查看已经被合并到当前分支的分支
git br --no-merged # 查看尚未被合并到当前分支的分支
git co <branch> # 切换到某个分支
git co -b <new_branch> # 创建新的分支,并且切换过去
git co -b <new_branch> <branch> # 基于branch创建新的new_branch
git co $id # 把某次历史提交记录checkout出来,但无分支信息,切换到其他分支会自动删除
git co $id -b <new_branch> # 把某次历史提交记录checkout出来,创建成一个分支
git br -d <branch> # 删除某个分支
git br -D <branch> # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)
 分支合并和rebase
git merge <branch> # 将branch分支合并到当前分支
git merge origin/master --no-ff # 不要Fast-Foward合并,这样可以生成merge提交
git rebase master <branch> # 将master rebase到branch,
相当于:git co <branch> && git rebase master && git co master && git merge <branch>
 Git补丁管理(方便在多台机器上开发同步时用)
git diff > ../sync.patch # 生成补丁
git apply ../sync.patch # 打补丁
git apply --check ../sync.patch #测试补丁能否成功
 Git暂存管理
git stash # 暂存
git stash list # 列所有stash
git stash apply # 恢复暂存的内容
git stash drop # 删除暂存区
Git远程分支管理
git pull # 抓取远程仓库所有分支更新并合并到本地
git pull --no-ff # 抓取远程仓库所有分支更新并合并到本地,不要快进合并
git fetch origin # 抓取远程仓库更新
git merge origin/master # 将远程主分支合并到本地当前分支
git co --track origin/branch # 跟踪某个远程分支创建相应的本地分支
git co -b <local_branch> origin/<remote_branch> # 基于远程分支创建本地分支,功能同上
git push # push所有分支
git push origin master # 将本地主分支推到远程主分支
git push -u origin master # 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)
git push origin <local_branch> # 创建远程分支, origin是远程仓库名
git push origin <local_branch>:<remote_branch> # 创建远程分支
git push origin :<remote_branch> #先删除本地分支(git br -d <branch>),然后再push删除远程分支
Git远程仓库管理
git remote -v # 查看远程服务器地址和仓库名称
git remote show origin # 查看远程服务器仓库状态
git remote add origin git@ github:robbin/robbin_site.git # 添加远程仓库地址
git remote set-url origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址(用于修改远程仓库地址) 
git remote rm <repository> # 删除远程仓库
创建远程仓库
git clone --bare robbin_site robbin_site.git # 用带版本的项目创建纯版本仓库
scp -r my_project.git git@ git.csdn.net:~ # 将纯仓库上传到服务器上
mkdir robbin_site.git && cd robbin_site.git && git --bare init # 在服务器创建纯仓库
git remote add origin git@ github.com:robbin/robbin_site.git # 设置远程仓库地址
git push -u origin master # 客户端首次提交
git push -u origin develop # 首次将本地develop分支提交到远程develop分支,并且track
git remote set-head origin master # 设置远程仓库的HEAD指向master分支
也可以命令设置跟踪远程库和本地库
git branch --set-upstream master origin/master
git branch --set-upstream develop origin/develop


下面是借鉴的博客

http://www.cnblogs.com/x_wukong/p/5408275.html

http://blog.csdn.net/xiaoputao0903/article/details/23933589

http://www.cnblogs.com/cspku/articles/Git_cmds.html


本文出自 “Forand” 博客,请务必保留此出处http://853056088.blog.51cto.com/12966870/1958420


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章