利用git提交代码的方法步骤
一、首先需要下载git
查看电脑是否安装git,打开终端,输入git,回车如果输出如下,则代表已安装了git
如果未安装,则会输出:
按照提示输入:sudoapt-getinstallgit即可安装!!或者到此处下载:git下载,pkg包下载完成,双击安装。
输入命令:git--version可查看当前git版本
二.安装后需要一些配置
配置用户名和邮箱:
$gitconfig--globaluser.name"YourName" $gitconfig--globaluser.email"email@example.com"
使用--global修饰后设置的全局的用户,如果设置单个项目的用户,可cd到项目根目录下,执行如下命令:
$gitconfiguser.name"YourName" $gitconfiguser.email"email@example.com"
使用命令:gitconfig--list可查看当前用户信息以及其他的一些信息
$gitconfig--list core.excludesfile=/Users/mac/.gitignore_global difftool.sourcetree.cmd=opendiff"$LOCAL""$REMOTE" difftool.sourcetree.path= mergetool.sourcetree.cmd=/Applications/SourceTree.app/Contents/Resources/opendiff-w.sh"$LOCAL""$REMOTE"-ancestor"$BASE"-merge"$MERGED" mergetool.sourcetree.trustexitcode=true http.postbuffer=524288000 https.postbuffer=524288000 user.email=你的邮箱@qq.com user.name=你的用户名 macdeMacBook-Pro:~Artron_LQQ$
三.建立本地git仓库
1.cd到你的项目目录
$cd/Users/cjk/Desktop/myShop
2.然后,输入git命令:
$gitinit
输出如下:
$gitinit InitializedemptyGitrepositoryin/Users/cjk/Desktop/GitTest/.git/
创建了一个空的本地仓库.
3.将项目的所有文件添加到缓存中:
$gitadd.
gitadd.(注意,后面有个点)表示添加目录下所有文件到缓存库,如果只添加某个文件,只需把.换成你要添加的文件名即可;
4.将缓存中的文件Commit到git库
gitcommit-m"添加你的注释,一般是一些更改信息"
下面是第一次提交时的输出:
$gitcommit-m"添加项目" [master(root-commit)3102a38]添加项目 18fileschanged,1085insertions(+) createmode100644GitTest.xcodeproj/project.pbxproj createmode100644GitTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata createmode100644GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate createmode100644GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/GitTest.xcscheme createmode100644GitTest.xcodeproj/xcuserdata/Artron_LQQ.xcuserdatad/xcschemes/xcschememanagement.plist createmode100644GitTest/AppDelegate.h createmode100644GitTest/AppDelegate.m createmode100644GitTest/Assets.xcassets/AppIcon.appiconset/Contents.json createmode100644GitTest/Base.lproj/LaunchScreen.storyboard createmode100644GitTest/Base.lproj/Main.storyboard createmode100644GitTest/Info.plist createmode100644GitTest/ViewController.h createmode100644GitTest/ViewController.m createmode100644GitTest/main.m createmode100644GitTestTests/GitTestTests.m createmode100644GitTestTests/Info.plist createmode100644GitTestUITests/GitTestUITests.m createmode100644GitTestUITests/Info.plist
或者不添加注释gitcommit,但是这样会进入vim(vi)编辑器
#Pleaseenterthecommitmessageforyourchanges.Linesstarting #with'#'willbeignored,andanemptymessageabortsthecommit. #Onbranchmaster #Changestobecommitted: #modified:LQQCircleShowImage.xcodeproj/project.pbxproj #modified:LQQCircleShowImage/TableViewCell.m # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "~/Desktop/LQQCircleShowImage/.git/COMMIT_EDITMSG"8L,292C
在这里可以输入更改信息,也可以不输入,然后按住shift+:,输入wq即可保存信息并退出vim编辑器;
四,建立远程库
在一些代码托管平台创建项目,例如github或者开源中国社区,这里已开源中国社区为例;
创建项目后,会生成一个HTTPS链接,如下:
https://git.oschina.net/liuqiqiang/gitTest.git
五,将本地的库链接到远
终端中输入:gitremoteaddoriginHTTPS链接
$gitremoteaddoriginhttps://git.oschina.net/liuqiqiang/gitTest.git
六.上传代码到远程库,上传之前最好先Pull一下,再执行命令:gitpulloriginmaster
输出:
$gitpulloriginmaster warning:nocommoncommits remote:Countingobjects:3,done. remote:Total3(delta0),reused0(delta0) Unpackingobjects:100%(3/3),done. Fromhttps://git.oschina.net/liuqiqiang/gitTest *branchmaster->FETCH_HEAD *[newbranch]master->origin/master Mergemadebythe'recursive'strategy. README.md|1+ 1filechanged,1insertion(+) createmode100644README.md
即pull成功,
七.接着执行:gitpushoriginmaster
完成后输出:
$gitpushoriginmaster Countingobjects:34,done. Deltacompressionusingupto4threads. Compressingobjects:100%(29/29),done. Writingobjects:100%(34/34),15.63KiB|0bytes/s,done. Total34(delta3),reused0(delta0) Tohttps://git.oschina.net/liuqiqiang/gitTest.git 5e2dda1..537ecfemaster->master
即将代码成功提交到远程库!!!
注:如果pull之后出现“refusingtomergeunrelatedhistories”这句,就证明你合并pull两个不同的项目
出现的问题如何去解决fatal:refusingtomergeunrelatedhistories
我在Github新建一个仓库,写了License,然后把本地一个写了很久仓库上传。
先pull,因为两个仓库不同,发现refusingtomergeunrelatedhistories,无法pull
因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在gitpull,这句代码是在git2.9.2版本发生的,最新的版本需要添加--allow-unrelated-histories
假如我们的源是origin,分支是master,那么我们需要这样写gitpulloriginmaster--allow-unrelated-histories需要知道,我们的源可以是本地的路径
接着到你的远程库查看,提交前:
提交成功后:
注意:操作的时候,指令不要输错了!!!!
下面这个是输错了orgin的输出:
gitpullorginmaster fatal:'orgin'doesnotappeartobeagitrepository fatal:Couldnotreadfromremoterepository. Pleasemakesureyouhavethecorrectaccessrights andtherepositoryexists.
正确的应该是origin!!
如果在push的时候有如下输出:
$gitpush-uoriginmaster Tohttps://git.oschina.net/liuqiqiang/LQQCircleShowImage.git ![rejected]master->master(fetchfirst) error:failedtopushsomerefsto'https://git.oschina.net/liuqiqiang/LQQCircleShowImage.git' hint:Updateswererejectedbecausetheremotecontainsworkthatyoudo hint:nothavelocally.Thisisusuallycausedbyanotherrepositorypushing hint:tothesameref.Youmaywanttofirstintegratetheremotechanges hint:(e.g.,'gitpull...')beforepushingagain. hint:Seethe'Noteaboutfast-forwards'in'gitpush--help'fordetails.
看提示可知道,需要先pull一下,即执行一次:gitpulloriginmaster
然后再执行:gitpushoriginmaster
分支管理
新建分支
$gitbranchnewbranch
查看分支
$gitbranch
输出:
*master newbranch
*代表当前所在的分支
切换分支
$gitcheckoutnewbranch
输出
Switchedtobranch'newbranch'
切换后可用gitbranch查看是否切换到当前分支
master *newbranch
提交改动到当前分支
$gitadd. $gitcommit-a
可使用gitstatus查看提交状态
接着切回主分支
$gitcheckoutmaster
输出:
Switchedtobranch'master'
将新分支提交的改动合并到主分支上
$gitmergenewbranch
输出:
Updatingcc73a48..93a1347
Fast-forward
GitTest.xcodeproj/project.pbxproj|9+++++++++
.../UserInterfaceState.xcuserstate|Bin0->7518bytes
GitTest/test.h|13+++++++++++++
GitTest/test.m|13+++++++++++++
4fileschanged,35insertions(+)
createmode100644GitTest.xcodeproj/project.xcworkspace/xcuserdata/Artron_LQQ.xcuserdatad/UserInterfaceState.xcuserstate
createmode100644GitTest/test.h
createmode100644GitTest/test.m
这里我提交了两个文件,即:test.h和test.m
如果合并后产生冲突,可输入以下指令查看冲突:
$gitdiff
修改之后,再次提交即可;
接下来,就可以push代码了:
$gitpush-uoriginmaster
这时可能需要你输入你的github用户名和密码,按照提示输入即可;
删除分支
$gitbranch-Dnewbranch
输出
Deletedbranchnewbranch(was93a1347).
以上就是最简单的github操作了,也是在网上看着学的,注意在实际操作中多加练习,代码这东西,刚开始桥的多了也就记下了!
到此这篇关于利用git提交代码的方法步骤的文章就介绍到这了,更多相关git提交代码内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。