设为首页 收藏本站
查看: 764|回复: 0

[经验分享] github创建远程仓库和git常用命令

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-8-23 09:01:53 | 显示全部楼层 |阅读模式
git创建远程仓库
  首先到github页面上创建仓库(repository)如下:
QQ截图20170823090140.png
  然后初始化文件夹为仓库,并提交到远程仓库,如下:
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
[iyunv@hxy aa]# git init
Initialized empty Git repository in /data/mydata/aa/.git/
[iyunv@hxy aa]# git add .
[iyunv@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
[iyunv@hxy aa]# git remote add origin git@github.com:mghxy123/test.git
[iyunv@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
[iyunv@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 git@github.com:mghxy123/test.git
* [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[iyunv@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
[iyunv@hxy aa]# git clone git@github.com:mghxy123/test1.git
Initialized empty Git repository in /data/mydata/aa/test1/.git/
warning: You appear to have cloned an empty repository.
[iyunv@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
[iyunv@hxy aa]# cd test1/
[iyunv@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
[iyunv@hxy test1]# cp ../README ./
[iyunv@hxy test1]# ls
README
[iyunv@hxy test1]# ls
README
[iyunv@hxy test1]# git add .
[iyunv@hxy test1]# git commit -m "add README"
[master (root-commit) d57f411] add README
1 files changed, 292 insertions(+), 0 deletions(-)
create mode 100644 README
[iyunv@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 'git@github.com:mghxy123/test1.git'
[iyunv@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 git@github.com: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






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-403347-1-1.html 上篇帖子: gitlab+jenkins+maven+docker持续集成(二)——maven安装配置 下篇帖子: Jenkins + Git + Maven + tomcat集成环境搭建
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表