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

[经验分享] Git使用、Git配置、Git提交代码、Git上传

[复制链接]

尚未签到

发表于 2018-1-15 19:14:57 | 显示全部楼层 |阅读模式
  非教程,只是自己的一个简单笔记。建议没有入门的朋友,直接看git的官方help文档:
  https://help.github.com/articles/set-up-git
  1、注册一个git账号,超级简单。
  2、直接在页面上,创建一个仓库(repo)
  3、根据https://help.github.com/articles/set-up-git的提示,安装一个客户端软件。然后安装、登录。
  4、如果是用公司的代理上网,设置代理的方式如下:
  打开Git Shell命令行
  依次输入
  git config --global http.proxy http://173.34.23.98:8080
  git config --global https.proxy http://173.34.23.98:8080
  git config --global http.sslverify false
  (其中http://开头的是代理服务器地址,自行更改)
  查看配置:
  git config --list
  5、同步你刚才创建的仓库。
  直接图形化界面上,点击clone。(最好是先在软件的设置那里,手动改一下它默认的目录,改成D盘)
  也可以用命令去clone,clone的意思就是去下载代码。
  

git clone https://github.com/username/Spoon-Knife.git  

  6、提交代码
  简单的讲,就是先新建一个文件(或者你自己拷贝一个到项目目录下面)
  用命令行操作,(图形化操作也可以,不介绍了)
  cd 切换到项目目录下。
  cd zollty-log
  然后
  add 添加新建的文件。
  git add CHANGE-LOG.txt
  然后再commit确认这个文件。
  git commit
  然后是push 同步到服务器端,
  

git push origin master  

  其中master是分支名称。
  通常有五个步骤:
  1.查看目前代码的修改状态
  2.查看代码修改内容
  3.暂存需要提交的文件
  4.提交已暂存的文件
  5.同步到服务器
  提交代码之前,首先应该检查目前所做的修改,运行git status命令
  a) 已暂存 (changes to be committed)
  new file //表示新建文件
  modified //表示修改文件
  deleted //表示删除文件
  b)       已修改 (changed but not updated)
  modified //表示修改文件
  deleted //表示删除文件
  另外,git 给出了可能需要的操作命令,git add/rm, gitcheckout --
  c)        未跟踪 (untracked files)

2.     查看代码修改的内容

  git diff  <file>
  比较某文件与最近提交节点的差异。
  注意:如果该文件已暂存,那么应该使用git diff –cached<file>
  git diff <hashcode> <hashcode>  <file>
  比较某文件在提交节点a,节点b的差异。
  技巧:如果省略后面一个hashcode,则默认表示与上一提交节点比较。(也可以利用^运算符)


3.     暂存需要提交的文件

如果是新建的文件  

  则git add  <file>
  如果是修改的文件
  则git add  <file>
  如果是删除的文件
  则 git rm  <file>


4.     提交已暂存的文件

  git commit
  注意注释填写规范。
  git commit --amend
  修改最近一次提交。有时候如果提交注释书写有误或者漏提文件,可以使用此命令。


5.     同步到服务器

  同步到服务器前先需要将服务器代码同步到本地
  命令: git pull
  如果执行失败,就按照提示还原有冲突的文件,然后再次尝试同步。
  命令:git checkout -- <有冲突的文件路径>
  同步到服务器
  命令: git push origin  <本地分支名>
  如果执行失败,一般是没有将服务器代码同步到本地导致的,先执行上面的git pull命令。

  二、.gitignore配置
  可以去https://github.com/github/gitignore下载很多模板。
  比如java的,php的,eclipse的。有些文件或目录应该在上传的时候过滤掉。比如.settings
  java的配置:
  *.class
  # Package Files #
  *.jar
  *.war
  *.ear
  eclipse的配置:

*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# PDT-specific
.buildpath  1、配置语法:
  以斜杠“/”开头表示目录;
  以星号“*”通配多个字符;
  以问号“?”通配单个字符
  以方括号“[]”包含单个字符的匹配列表;
  以叹号“!”对匹配结果取反;
  此外,git 对于 .ignore 配置文件是按行从上到下进行规则匹配的,意味着如果前面的规则匹配的范围更大,则后面的规则将不会生效;
  2、示例:
  (1)规则:fd1/*
  说明:忽略目录 fd1 下的全部内容;注意,不管是根目录下的 /fd1/ 目录,还是某个子目录 /child/fd1/ 目录,都会被忽略;
  (2)规则:/fd1/*
  说明:忽略根目录下的 /fd1/ 目录的全部内容;
  (3)规则:
  /*
  !.gitignore
  !/fw/bin/
  !/fw/sf/
  说明:忽略全部内容,但是不忽略 .gitignore 文件、根目录下的 /fw/bin/ 和 /fw/sf/ 目录;
  三、Fork A Repo(在已有的项目上建立自己的仓库 [ 即fork ])
  部分文档翻译、整理的内容:(红色字体是我的注释)
  以项目名Spoon-Knife为例:
  If you've found yourself on this page, we're assuming you're brand new to Git and GitHub. This guide will walk you through the basics and explain a little bit about how everything works along the way.

Contributing to a project
  At some point you may find yourself wanting to contribute to someone else's project, or would like to use someone's project as the starting point for your own. This is known as "forking". For this tutorial, we'll be using theSpoon-Knifeproject, hosted on GitHub.com.

Step 1: Fork the "Spoon-Knife" repository(第一步,找到这个项目,点击 fork图标)
  To fork this project, click the "Fork" button in the GitHub.com repository.


Step 2: Clone your fork(第二步,在shell窗口中输入如下命令)
  You've successfully forked the Spoon-Knife repository, but so far it only exists on GitHub. To be able to work on the project, you will need to clone it to your local machine.
  Run the following code:(命令如下)
  

git clone https://github.com/username/Spoon-Knife.git(注意要替换成自己的username) # Clones your fork of the repository into the current directory in terminal  

  

Step 3: Configure remotes(第三步,配置一个映射到原始项目的流(upstream),用于从原始项目获取更新,输入下面两个命令)
  When a repository is cloned, it has a default remote calledoriginthat points to your fork on GitHub, not the original repository it was forked from. To keep track of the original repository, you need to add another remote namedupstream:

More about remotes
  

cd Spoon-Knife  
# Changes the active directory in the prompt to the newly cloned "Spoon-Knife" directory
  
git remote add upstream https://github.com/octocat/Spoon-Knife.git(命令1) # Assigns the original repository to a remote called "upstream"
  
git fetch upstream(命令2) # Pulls in changes not present in your local repository, without modifying your files
  

  

More Things You Can Do(more)
  You've successfully forked a repository, but get a load of these other cool things you can do:

Pull in upstream changes(将原始项目中的更新合并到自己的master分支上)
  If the original repository you forked your project from gets updated, you can add those updates to your fork by running the following code:
  

git fetch upstream  
# Fetches any new changes from the original repository
  
git merge upstream/master
  
# Merges any changes fetched into your working files
  

  

Push commits(提交本地的更新(到本地master分支上)
  Step 2: Commit your README
  Now that you have your README set up, it's time to commit it. A commit is essentially a snapshot of all the files in your project at a particular point in time. In the prompt, type the following code:

More about commits
  

git add README  
# Stages your README file, adding it to the list of files to be committed
  

  
git commit -m 'first commit'
  
# Commits your files, adding the message "first commit"
  

  

Step 3: Push your commit
  So far, everything you've done has been in your local repository, meaning you still haven't done anything on GitHub yet. To connect your local repository to your GitHub account, you will need to set a remote for your repository and push your commits to it.

More about remotes
  

git remote add origin https://github.com/username/Hello-World.git  
# Creates a remote named "origin" pointing at your GitHub repository
  

  
git push origin master
  
# Sends your commits in the "master" branch to GitHub
  



What is the difference between fetch and pull?



Create branches

  Branching allows you to build new features or test out>
How do I use branches?



Pull requests
  If you are hoping to contribute back to the original fork, you can send the original author apull request.

Unwatch the main repository
  When you fork a particularly popular repository, you may find yourself with a lot of unwanted updates about it. To unsubscribe from updates to the main repository, click the "Unwatch" button on themain repositoryand select "Not Watching".

运维网声明 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-435446-1-1.html 上篇帖子: [Git] 给git命令起别名 下篇帖子: Cygwin 安装 git
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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