利用git提交代码的方法步骤

时间:2021-10-06 06:02:31

一、首先需要下载git

  查看电脑是否安装git,打开终端,输入git,回车如果输出如下,则代表已安装了git

利用git提交代码的方法步骤

如果未安装,则会输出:

利用git提交代码的方法步骤

按照提示输入:sudo apt-get install git即可安装!!或者到此处下载:,pkg包下载完成,双击安装。

输入命令:git --version 可查看当前git版本

利用git提交代码的方法步骤

二.安装后需要一些配置

配置用户名和邮箱:

?
1
2
$ git config --global user.name "your name"
$ git config --global user.email "email@example.com"

使用 --global 修饰后设置的全局的用户,如果设置单个项目的用户,可cd到项目根目录下,执行如下命令:

?
1
2
$ git config user.name "your name"
$ git config user.email "email@example.com"

使用命令:git config --list 可查看当前用户信息以及其他的一些信息

?
1
2
3
4
5
6
7
8
9
10
11
$ git config --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到你的项目目录

?
1
$ cd /users/cjk/desktop/myshop

2. 然后,输入git命令:

?
1
$ git init

输出如下:

?
1
2
$ git init
initialized empty git repository in /users/cjk/desktop/gittest/.git/

创建了一个空的本地仓库.

3.将项目的所有文件添加到缓存中:

?
1
$ git add .

git add . (注意,后面有个点)表示添加目录下所有文件到缓存库,如果只添加某个文件,只需把 . 换成你要添加的文件名即可;

4.将缓存中的文件commit到git库

git commit -m "添加你的注释,一般是一些更改信息"

下面是第一次提交时的输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ git commit -m "添加项目"
[master (root-commit) 3102a38] 添加项目
 18 files changed, 1085 insertions(+)
 create mode 100644 gittest.xcodeproj/project.pbxproj
 create mode 100644 gittest.xcodeproj/project.xcworkspace/contents.xcworkspacedata
 create mode 100644 gittest.xcodeproj/project.xcworkspace/xcuserdata/artron_lqq.xcuserdatad/userinterfacestate.xcuserstate
 create mode 100644 gittest.xcodeproj/xcuserdata/artron_lqq.xcuserdatad/xcschemes/gittest.xcscheme
 create mode 100644 gittest.xcodeproj/xcuserdata/artron_lqq.xcuserdatad/xcschemes/xcschememanagement.plist
 create mode 100644 gittest/appdelegate.h
 create mode 100644 gittest/appdelegate.m
 create mode 100644 gittest/assets.xcassets/appicon.appiconset/contents.json
 create mode 100644 gittest/base.lproj/launchscreen.storyboard
 create mode 100644 gittest/base.lproj/main.storyboard
 create mode 100644 gittest/info.plist
 create mode 100644 gittest/viewcontroller.h
 create mode 100644 gittest/viewcontroller.m
 create mode 100644 gittest/main.m
 create mode 100644 gittesttests/gittesttests.m
 create mode 100644 gittesttests/info.plist
 create mode 100644 gittestuitests/gittestuitests.m
 create mode 100644 gittestuitests/info.plist

或者不添加注释 git commit ,但是这样会进入vim(vi)编辑器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# please enter the commit message for your changes. lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# on branch master
# changes to be committed:
#    modified:  lqqcircleshowimage.xcodeproj/project.pbxproj
#    modified:  lqqcircleshowimage/tableviewcell.m
#
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
~                                       
"~/desktop/lqqcircleshowimage/.git/commit_editmsg" 8l, 292c

在这里可以输入更改信息,也可以不输入,然后 按住 shift + : ,输入wq 即可保存信息并退出vim编辑器;

四,建立远程库

在一些代码托管平台创建项目,例如github或者开源中国社区,这里已开源中国社区为例;

创建项目后,会生成一个https链接,如下:

利用git提交代码的方法步骤

https://git.oschina.net/liuqiqiang/gittest.git

五,将本地的库链接到远

终端中输入: git remote add originhttps链接

?
1
$ git remote add origin https://git.oschina.net/liuqiqiang/gittest.git

六.上传代码到远程库,上传之前最好先pull一下,再执行命令: git pull origin master

输出:

?
1
2
3
4
5
6
7
8
9
10
11
12
$ git pull origin master
warning: no common commits
remote: counting objects: 3, done.
remote: total 3 (delta 0), reused 0 (delta 0)
unpacking objects: 100% (3/3), done.
from https://git.oschina.net/liuqiqiang/gittest
 * branch      master   -> fetch_head
 * [new branch]   master   -> origin/master
merge made by the 'recursive' strategy.
 readme.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 readme.md

即pull成功,

七.接着执行:git push origin master

完成后输出:

?
1
2
3
4
5
6
7
8
$ git push origin master
counting objects: 34, done.
delta compression using up to 4 threads.
compressing objects: 100% (29/29), done.
writing objects: 100% (34/34), 15.63 kib | 0 bytes/s, done.
total 34 (delta 3), reused 0 (delta 0)
to https://git.oschina.net/liuqiqiang/gittest.git
  5e2dda1..537ecfe master -> master

即将代码成功提交到远程库!!!

注:如果pull之后出现 “ refusing to merge unrelated histories ”这句,就证明你合并pull两个不同的项目

出现的问题如何去解决fatal: refusing to merge unrelated histories

我在github新建一个仓库,写了license,然后把本地一个写了很久仓库上传。

先pull,因为两个仓库不同,发现refusing to merge unrelated histories,无法pull

因为他们是两个不同的项目,要把两个不同的项目合并,git需要添加一句代码,在git pull,这句代码是在git 2.9.2版本发生的,最新的版本需要添加--allow-unrelated-histories

假如我们的源是origin,分支是master,那么我们 需要这样写git pull origin master --allow-unrelated-histories需要知道,我们的源可以是本地的路径

接着到你的远程库查看,提交前:

利用git提交代码的方法步骤

提交成功后:

利用git提交代码的方法步骤

注意:操作的时候,指令不要输错了!!!!

下面这个是输错了 orgin的输出:

?
1
2
3
4
5
6
git pull orgin master
fatal: 'orgin' does not appear to be a git repository
fatal: could not read from remote repository.
 
please make sure you have the correct access rights
and the repository exists.

正确的应该是origin!!

如果在push的时候有如下输出:

?
1
2
3
4
5
6
7
8
9
$ git push -u origin master
to https://git.oschina.net/liuqiqiang/lqqcircleshowimage.git
 ! [rejected]    master -> master (fetch first)
error: failed to push some refs to 'https://git.oschina.net/liuqiqiang/lqqcircleshowimage.git'
hint: updates were rejected because the remote contains work that you do
hint: not have locally. this is usually caused by another repository pushing
hint: to the same ref. you may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: see the 'note about fast-forwards' in 'git push --help' for details.

看提示可知道,需要先pull一下,即执行一次:git pull origin master

然后再执行:git push origin master

分支管理

新建分支

?
1
$ git branch newbranch

查看分支

?
1
$ git branch

输出:

?
1
2
* master
newbranch

*代表当前所在的分支

切换分支

?
1
$ git checkout new branch

输出

?
1
switched to branch 'newbranch'

切换后可用git branch查看是否切换到当前分支

?
1
2
master
* newbranch

提交改动到当前分支

?
1
2
$ git add .
$ git commit -a

可使用git status查看提交状态

接着切回主分支

?
1
$ git checkout master

输出:

?
1
switched to branch 'master'

将新分支提交的改动合并到主分支上

?
1
$ git merge newbranch

输出:

updating cc73a48..93a1347
fast-forward
gittest.xcodeproj/project.pbxproj | 9 +++++++++
.../userinterfacestate.xcuserstate | bin 0 -> 7518 bytes
gittest/test.h | 13 +++++++++++++
gittest/test.m | 13 +++++++++++++
4 files changed, 35 insertions(+)
create mode 100644 gittest.xcodeproj/project.xcworkspace/xcuserdata/artron_lqq.xcuserdatad/userinterfacestate.xcuserstate
create mode 100644 gittest/test.h
create mode 100644 gittest/test.m

这里我提交了两个文件,即:test.h和test.m

如果合并后产生冲突,可输入以下指令查看冲突:

?
1
$ git diff

修改之后,再次提交即可;

接下来,就可以push代码了:

?
1
$ git push -u origin master

这时可能需要你输入你的github用户名和密码,按照提示输入即可;

删除分支

?
1
$ git branch -d newbranch

输出

deleted branch newbranch (was 93a1347).

以上就是最简单的github操作了,也是在网上看着学的,注意在实际操作中多加练习,代码这东西,刚开始桥的多了也就记下了!

到此这篇关于利用git提交代码的方法步骤的文章就介绍到这了,更多相关git 提交代码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/jackchensir/p/8306448.html