Git使用、Git配置、Git提交代码、Git上传

时间:2023-03-09 01:02:36
Git使用、Git配置、Git提交代码、Git上传

非教程,只是自己的一个简单笔记。建议没有入门的朋友,直接看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.

Git使用、Git配置、Git提交代码、Git上传

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:

 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:

 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.

 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

Create branches

Branching allows you to build new features or test out ideas without putting your main project at risk. In git, branch is a sort of bookmark that references the last commit made in the branch. This makes branches very small and easy to work with.

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".