Git简介及入门

来源:互联网 发布:java socket 中断 编辑:程序博客网 时间:2024/05/29 08:55


转至元数据结尾转至元数据起始

  • 1为何使用Git
  • 2Git介绍
    • 2.1术语表
    • 2.2基本概念
      • 2.2.1何谓分布式的SCM?
      • 2.2.2Git常用命令及作弊纸
    • 2.3安装Git及TortoiseGit(Windows用户)
      • 2.3.1TortoiseGit
    • 2.4获取并配置GitLab的账号
    • 2.5拉取并创建本地代码仓库

为何使用Git

Git的主要优点:

  1. 对非线性开发有非常强的支持
  2. 分布式、协作式开发处理代码冲突的能力强
  3. 数据结构更优,更适合管理大规模工程
  4. 社区更活跃,生态更完整(见上图)

参考Git Wiki:https://en.wikipedia.org/wiki/Git

Git介绍

术语表

Git术语
解释
branchA "branch" is an active line of development. The most recent commit on a branch is referred to as the tip of that branch. The tip of the branch is referenced by a branch head, which moves forward as additional development is done on the branch. A single Git repository can track an arbitrary number of branches, but your working tree is associated with just one of them (the "current" or "checked out" branch), and HEAD points to that branch.checkoutThe action of updating all or part of the working tree with a tree object or blob from the object database, and updating the index and HEAD if the whole working tree has been pointed at a new branch.cherry-pickingIn SCM jargon, "cherry pick" means to choose a subset of changes out of a series of changes (typically commits) and record them as a new series of changes on top of a different codebase. In Git, this is performed by the "git cherry-pick" command to extract the change introduced by an existing commit and to record it based on the tip of the current branch as a new commit.cleanA working tree is clean, if it corresponds to the revision referenced by the current head. Also see "dirty".commit

As a noun: A single point in the Git history; the entire history of a project is represented as a set of interrelated commits. The word "commit" is often used by Git in the same places other revision control systems use the words "revision" or "version". Also used as a short hand for commit object.

As a verb: The action of storing a new snapshot of the project’s state in the Git history, by creating a new commit representing the current state of the index and advancing HEAD to point at the new commit.

dirtyA working tree is said to be "dirty" if it contains modifications which have not been committed to the current branch.fast-forwardA fast-forward is a special type of merge where you have a revision and you are "merging" another branch's changes that happen to be a descendant of what you have. In such a case, you do not make a new merge commit but instead just update to his revision. This will happen frequently on a remote-tracking branch of a remote repository.fetchFetching a branch means to get the branch’s head ref from a remote repository, to find out which objects are missing from the local object database, and to get them, too. See also git-fetch(1).gitfileA plain file .git at the root of a working tree that points at the directory that is the real repository.HEADThe current branch. In more detail: Your working tree is normally derived from the state of the tree referred to by HEAD. HEAD is a reference to one of the heads in your repository, except when using a detached HEAD, in which case it directly references an arbitrary commit.masterThe default development branch. Whenever you create a Git repository, a branch named "master" is created, and becomes the active branch. In most cases, this contains the local development, though that is purely by convention and is not required.merge

As a verb: To bring the contents of another branch (possibly from an external repository) into the current branch. In the case where the merged-in branch is from a different repository, this is done by first fetching the remote branch and then merging the result into the current branch. This combination of fetch and merge operations is called a pull. Merging is performed by an automatic process that identifies changes made since the branches diverged, and then applies all those changes together. In cases where changes conflict, manual intervention may be required to complete the merge.

As a noun: unless it is a fast-forward, a successful merge results in the creation of a new commit representing the result of the merge, and having as parents the tips of the merged branches. This commit is referred to as a "merge commit", or sometimes just a "merge".

originThe default upstream repository. Most projects have at least one upstream project which they track. By default origin is used for that purpose. New upstream updates will be fetched into remote-tracking branches named origin/name-of-upstream-branch, which you can see using git branch -r.pullPulling a branch means to fetch it and merge it. See also git-pull(1).pushPushing a branch means to get the branch’s head ref from a remote repository, find out if it is a direct ancestor to the branch’s local head ref, and in that case, putting all objects, which are reachable from the local head ref, and which are missing from the remote repository, into the remote object database, and updating the remote head ref. If the remote head is not an ancestor to the local head, the push fails.rebaseTo reapply a series of changes from a branch to a different base, and reset the head of that branch to the result.repositoryA collection of refs together with an object database containing all objects which are reachable from the refs, possibly accompanied by meta data from one or more porcelains. A repository can share an object database with other repositories via alternates mechanism.resolveThe action of fixing up manually what a failed automatic merge left behind.


也可以在已安装Git的console输入命令`git help glossary`查看完整的术语表。

基本概念

何谓分布式的SCM?

与SVN不同,每一个developer本地的代码仓库(repository)都是一个功能完整,历史完整的仓库,它们是完全等价的。因此,在Git的开发流程中,我们会把其中一个代码仓库人为地定义为blessed repository(受庇佑的代码仓库)来作为中心代码仓库。

Workflow A

SVN的中心化的开发流程

Workflow C

由于Git分布式的特性,Git可以有更精细的代码控制工作流。

必须理解的Git概念 - Staging Area (暂存区)

Index 1

所有的代码编辑都发生在working directory,而所有的commit都发生在staging area。所以用户必须先把要提交的代码添加到暂存区。

当然,用户也可以使用`git commit -a`命令,把所有working directory的编辑都直接提交到仓库。

Staging area的存在,提供给用户更精细的代码版本管控。例如在同一个文件进行两个完全不相关的改动时,可以快速地把一部分先添加到暂存区,而不必担心这些改动会被后续的另一项改动所覆盖。

必须理解的Git概念 - Remote "Origin"

上面已经提到,Git是分布式的,出本地以外会存在很多个等价的代码仓库。因此,在本地的配置文件中(注意这不是代码仓库的一部分),会以remote字段记录其他远程代码仓库的别称。“origin”是其中一个最常见的别称,他通常标记着我们“blessed repository”的地址。

Git常用命令及作弊纸

https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf



iBest Git工作流

安装Git及TortoiseGit(Windows用户)

Git for Windows

  1. 下载:https://git-for-windows.github.io/
  2. 安装,注意“Choosing the SSH executable”选择“Use(Tortoise)Plink”,其他步骤均选择默认选项即可:

TortoiseGit

  1. 下载:https://tortoisegit.org/download/;
  2. (可选)有需要的同学可以一并下载中文语言包;
  3. 安装后开启First Start Wizard(需要语言包的童鞋需要在这步安装语言包然后点“Refresh”按钮哦~):
  4. 这一步请填写公司邮箱:
  5. 点击“Generate PuTTY key pair” - “Generate” - 然后开始在对话框空白的地方画圈圈/签个名啥的,最后保存公钥、私钥以备后续使用:


  6. 点击“完成”,结束向导。

获取并配置GitLab的账号

  1. 访问:http://carlos-hp/users/sign_in#register-pane;
  2. 以公司邮箱填写注册信息并点击“Register”;
  3. 联系team leader把你添加到对应的工程项目组,完成后你将见到这样的画面:
  4. 添加SSH Keys。你的SSH Keys将是你访问远端Git仓库的凭证,请小心保管,切勿外泄:
    1. 前往:http://carlos-hp/profile/keys;
    2. 找到你本地的SSH公钥(注意千万不要提供私钥)。公钥的地址通常在`~/.ssh/id_rsa.pub`。Windows用户请遵循上一节安装TortoiseGit的说明,填入保存的公钥;
    3. 复制公钥的地址到输入框,并点击“Add Key”。

拉取并创建本地代码仓库

  1. 配置Git用户:

    git config --global user.name "严昭"
    git config --global user.email "z.yan@itsz.cn"
  2. 配置Pageant使用正确的秘钥:

    1. 打开Pageant:
    2. 邮件点击托盘图标,点击Add Key:
    3. 选择刚才保存的私钥,点击完成。

  3. 拉取远端仓库:

    # 把your-project替换为你的项目名,如ib-android
    git clone git@carlos-hp:ibest/your-project.git
  4. Happy Coding!

转至元数据结尾
转至元数据起始

  • 1为何使用Git
  • 2Git介绍
    • 2.1术语表
    • 2.2基本概念
      • 2.2.1何谓分布式的SCM?
      • 2.2.2Git常用命令及作弊纸
    • 2.3安装Git及TortoiseGit(Windows用户)
      • 2.3.1TortoiseGit
    • 2.4获取并配置GitLab的账号
    • 2.5拉取并创建本地代码仓库

为何使用Git

Git的主要优点:

  1. 对非线性开发有非常强的支持
  2. 分布式、协作式开发处理代码冲突的能力强
  3. 数据结构更优,更适合管理大规模工程
  4. 社区更活跃,生态更完整(见上图)

参考Git Wiki:https://en.wikipedia.org/wiki/Git

Git介绍

术语表

Git术语
解释
branchA "branch" is an active line of development. The most recent commit on a branch is referred to as the tip of that branch. The tip of the branch is referenced by a branch head, which moves forward as additional development is done on the branch. A single Git repository can track an arbitrary number of branches, but your working tree is associated with just one of them (the "current" or "checked out" branch), and HEAD points to that branch.checkoutThe action of updating all or part of the working tree with a tree object or blob from the object database, and updating the index and HEAD if the whole working tree has been pointed at a new branch.cherry-pickingIn SCM jargon, "cherry pick" means to choose a subset of changes out of a series of changes (typically commits) and record them as a new series of changes on top of a different codebase. In Git, this is performed by the "git cherry-pick" command to extract the change introduced by an existing commit and to record it based on the tip of the current branch as a new commit.cleanA working tree is clean, if it corresponds to the revision referenced by the current head. Also see "dirty".commit

As a noun: A single point in the Git history; the entire history of a project is represented as a set of interrelated commits. The word "commit" is often used by Git in the same places other revision control systems use the words "revision" or "version". Also used as a short hand for commit object.

As a verb: The action of storing a new snapshot of the project’s state in the Git history, by creating a new commit representing the current state of the index and advancing HEAD to point at the new commit.

dirtyA working tree is said to be "dirty" if it contains modifications which have not been committed to the current branch.fast-forwardA fast-forward is a special type of merge where you have a revision and you are "merging" another branch's changes that happen to be a descendant of what you have. In such a case, you do not make a new merge commit but instead just update to his revision. This will happen frequently on a remote-tracking branch of a remote repository.fetchFetching a branch means to get the branch’s head ref from a remote repository, to find out which objects are missing from the local object database, and to get them, too. See also git-fetch(1).gitfileA plain file .git at the root of a working tree that points at the directory that is the real repository.HEADThe current branch. In more detail: Your working tree is normally derived from the state of the tree referred to by HEAD. HEAD is a reference to one of the heads in your repository, except when using a detached HEAD, in which case it directly references an arbitrary commit.masterThe default development branch. Whenever you create a Git repository, a branch named "master" is created, and becomes the active branch. In most cases, this contains the local development, though that is purely by convention and is not required.merge

As a verb: To bring the contents of another branch (possibly from an external repository) into the current branch. In the case where the merged-in branch is from a different repository, this is done by first fetching the remote branch and then merging the result into the current branch. This combination of fetch and merge operations is called a pull. Merging is performed by an automatic process that identifies changes made since the branches diverged, and then applies all those changes together. In cases where changes conflict, manual intervention may be required to complete the merge.

As a noun: unless it is a fast-forward, a successful merge results in the creation of a new commit representing the result of the merge, and having as parents the tips of the merged branches. This commit is referred to as a "merge commit", or sometimes just a "merge".

originThe default upstream repository. Most projects have at least one upstream project which they track. By default origin is used for that purpose. New upstream updates will be fetched into remote-tracking branches named origin/name-of-upstream-branch, which you can see using git branch -r.pullPulling a branch means to fetch it and merge it. See also git-pull(1).pushPushing a branch means to get the branch’s head ref from a remote repository, find out if it is a direct ancestor to the branch’s local head ref, and in that case, putting all objects, which are reachable from the local head ref, and which are missing from the remote repository, into the remote object database, and updating the remote head ref. If the remote head is not an ancestor to the local head, the push fails.rebaseTo reapply a series of changes from a branch to a different base, and reset the head of that branch to the result.repositoryA collection of refs together with an object database containing all objects which are reachable from the refs, possibly accompanied by meta data from one or more porcelains. A repository can share an object database with other repositories via alternates mechanism.resolveThe action of fixing up manually what a failed automatic merge left behind.


也可以在已安装Git的console输入命令`git help glossary`查看完整的术语表。

基本概念

何谓分布式的SCM?

与SVN不同,每一个developer本地的代码仓库(repository)都是一个功能完整,历史完整的仓库,它们是完全等价的。因此,在Git的开发流程中,我们会把其中一个代码仓库人为地定义为blessed repository(受庇佑的代码仓库)来作为中心代码仓库。

Workflow A

SVN的中心化的开发流程

Workflow C

由于Git分布式的特性,Git可以有更精细的代码控制工作流。

必须理解的Git概念 - Staging Area (暂存区)

Index 1

所有的代码编辑都发生在working directory,而所有的commit都发生在staging area。所以用户必须先把要提交的代码添加到暂存区。

当然,用户也可以使用`git commit -a`命令,把所有working directory的编辑都直接提交到仓库。

Staging area的存在,提供给用户更精细的代码版本管控。例如在同一个文件进行两个完全不相关的改动时,可以快速地把一部分先添加到暂存区,而不必担心这些改动会被后续的另一项改动所覆盖。

必须理解的Git概念 - Remote "Origin"

上面已经提到,Git是分布式的,出本地以外会存在很多个等价的代码仓库。因此,在本地的配置文件中(注意这不是代码仓库的一部分),会以remote字段记录其他远程代码仓库的别称。“origin”是其中一个最常见的别称,他通常标记着我们“blessed repository”的地址。

Git常用命令及作弊纸

https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf



iBest Git工作流

安装Git及TortoiseGit(Windows用户)

Git for Windows

  1. 下载:https://git-for-windows.github.io/
  2. 安装,注意“Choosing the SSH executable”选择“Use(Tortoise)Plink”,其他步骤均选择默认选项即可:

TortoiseGit

  1. 下载:https://tortoisegit.org/download/;
  2. (可选)有需要的同学可以一并下载中文语言包;
  3. 安装后开启First Start Wizard(需要语言包的童鞋需要在这步安装语言包然后点“Refresh”按钮哦~):
  4. 这一步请填写公司邮箱:
  5. 点击“Generate PuTTY key pair” - “Generate” - 然后开始在对话框空白的地方画圈圈/签个名啥的,最后保存公钥、私钥以备后续使用:


  6. 点击“完成”,结束向导。

获取并配置GitLab的账号

  1. 访问:http://carlos-hp/users/sign_in#register-pane;
  2. 以公司邮箱填写注册信息并点击“Register”;
  3. 联系team leader把你添加到对应的工程项目组,完成后你将见到这样的画面:
  4. 添加SSH Keys。你的SSH Keys将是你访问远端Git仓库的凭证,请小心保管,切勿外泄:
    1. 前往:http://carlos-hp/profile/keys;
    2. 找到你本地的SSH公钥(注意千万不要提供私钥)。公钥的地址通常在`~/.ssh/id_rsa.pub`。Windows用户请遵循上一节安装TortoiseGit的说明,填入保存的公钥;
    3. 复制公钥的地址到输入框,并点击“Add Key”。

拉取并创建本地代码仓库

  1. 配置Git用户:

    git config --global user.name "严昭"
    git config --global user.email "z.yan@itsz.cn"
  2. 配置Pageant使用正确的秘钥:

    1. 打开Pageant:
    2. 邮件点击托盘图标,点击Add Key:
    3. 选择刚才保存的私钥,点击完成。

  3. 拉取远端仓库:

    # 把your-project替换为你的项目名,如ib-android
    git clone git@carlos-hp:ibest/your-project.git
  4. Happy Coding!

原创粉丝点击