Git 和 GitHub 新手入门 什么是版本控制系统?什么是 Git?Git 入门

2025-05-27

适合初学者的 Git 和 GitHub

什么是版本控制系统?

什么是 git?

GIT 入门

涵盖的主题

  1. 版本控制系统简介
  2. Git 和 GitHub
  3. 分支与合并
  4. 如何为开源做出贡献
  5. 如何同步你的 Repo

Git 和 GitHub 是每个软件开发者必学的工具。这些工具不仅能让你轻松追踪个人项目的变更,还能让你无缝地参与和协调团队项目。本指南旨在帮助每一位初学者掌握技能,轻松学习和使用这些工具。让我们开始吧。

什么是版本控制系统?

版本控制系统是软件开发人员用来跟踪和管理项目变更的工具。这些工具会记录所做的变更、更改者以及更改时间。因此,可以轻松返回文档的先前版本并识别出现的错误。

版本控制系统的类型

VCS 主要有三种类型,即:
• 本地版本控制系统
• 集中式版本控制系统
• 分布式版本控制系统

本地版本控制系统 (VC) 是一种使用计算机本地数据库来存储更改的 VC。它以补丁的形式存储更改,每个补丁都包含自先前版本以来对文件所做的更改,而不是整个文件。因此,要查看文件在任何情况下的版本,您必须添加所有相关的补丁才能生成文档。
低电压

集中式版本控制系统使用中央服务器来存储代码库。因此,每个开发人员都必须连接到中央服务器才能访问代码库并进行更改。虽然在服务器上维护单个代码库相当容易,但一旦中央服务器崩溃,您可能会面临数据丢失的风险,这是一个主要缺点。集中式版本控制系统的一个例子是Apache Subversion(简称 SVN)。
循环风险控制系统

分布式 VCS

与集中式版本控制系统不同,每个贡献者都拥有自己的主仓库本地副本。因此,他们可以在不干扰主仓库的情况下更改、更新和提交到本地仓库。贡献者通过克隆来复制主仓库。此外,他们还必须暂存、提交并推送更改,以便其他贡献者可以查看。
分布式风险控制系统

了解了版本控制系统之后,让我们深入研究一下 GIT

什么是 git?

Git 是一个开源分布式版本控制软件,由 Linus Torvalds 于 2005 年为 Linux 内核创建。它允许一组开发人员在一个项目上工作,每个开发人员都拥有位于中央服务器的主存储库副本。

Git 的功能

  1. 免费开源——您无需支付任何费用即可使用 Git。此外,它的源代码是公开的,因此您可以根据自己的喜好进行修改。
  2. 分布式开发 - Git 支持克隆,贡献者可以存储主存储库的本地副本
  3. 支持非线性开发——Git 支持独立的代码行(称为分支),这些代码行可以独立于主代码库进行暂存、提交和更新。因此,分支为开发人员提供了一个安全的空间来实施和测试新内容,而不会干扰代码库。分支之后可以合并到代码库中。
  4. 可扩展性——Git 具有相当强的可扩展性,可以轻松处理合作者数量的增加。
  5. 安全——此工具使用安全哈希函数 (SHA1) 来命名和识别存储库中的对象。因此,每个更改都受到严密监控,确保更改不会被察觉。##什么是 GitHub?GitHub是一家成立于 2008 年的托管公司,为开发人员提供托管和共享项目的平台。它为每个托管项目提供访问控制和管理功能。基本上,它提供源代码管理以及 Git 的其他功能。

GitHub 有很多替代品,例如 GitLab 和 bitbucket。因此,你不一定需要 GitHub 才能使用 git,但你需要 git 才能使用 GitHub。

GIT 入门

你需要在操作系统上安装 git。本指南将帮助你。
此外,创建一个 GitHub 帐户,然后我们继续。

git GitHub 工作原理及其命令

GitHub

让我们简单讨论一下这个图表;
在用户对工作文件进行更改后,git 会注意到最近修改的文件。用户可以使用git status命令检查修改后的文件。

修改后的文件被添加到暂存区,作为文件的临时位置。

所有已暂存的文件都已准备好提交/移动到本地仓库。目前,这些更改仅在开发者的本地计算机上可见,其他贡献者无法在线访问。
要使文件在线可用,您需要将文件托管在 GitHub 等在线托管服务上。因此,您需要推送本地仓库以将其与远程仓库同步。

假设其他贡献者已将更改推送到远程存储库。您必须将本地存储库与远程存储库同步,才能确保更改及时更新。为此,您可以使用Git Pull命令来同步本地存储库。
现在让我们看看 git 命令

1. Git 状态

git status命令列出了所有最近修改过但尚未添加到本地仓库的文件。以下示例中,Two Sum.md 文件尚未添加到本地仓库。

PS C:\Users\ERICA WANJA\Desktop\coding problems> git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        leetcode/Two Sum.md

nothing added to commit but untracked files present (use "git add" to track)
PS C:\Users\ERICA WANJA\Desktop\coding problems>
Enter fullscreen mode Exit fullscreen mode

2. Git 添加

git add命令用于将文件移动到暂存区进行提交。要暂存文件,请使用以下命令:
git add filename - 添加特定文件

git add -all缩写为git add -A暂存所有文件,即暂存新建、修改和删除的文件。另一方面,该git add .命令仅暂存新建和修改的文件,不包括已删除的文件。

3. Git 提交

git commit将仓库从暂存区移至本地仓库。换句话说,它会将所做的更改的快照存储到本地仓库,而不是盲目地再次复制整个仓库。
每次提交时,都必须提供一条简短的消息来解释所做的更改。
例如:

git commit –m “solved the two sum problem”
```


###4. Git pull 
The pull command helps to keep your local repo up-to-date with the remote repository. It up streams any change made by another contributor in the remote repository to your local repository.
To pull the changes, you need to set the origin or parent remote repository
Command:


```
git remote add origin LinkToTheRemoteRepo
```


After setting the origin remote repository, you can now pull the changes using the below command;


```
git pull origin master
```


**Note:** it is a good practice that you pull the changes before the push command when working on a team’s project.
###5. Git push
`git push` command transfers the commits changes from the local repository to the remote repository(GitHub). Thus, the changes you have made will be published on the central repository and made available online. 
Commands
`git push  origin main ` pushes the commits to the main or the master branch
`git push origin branchName` pushes the commits to the named branch.(you will learn about branches shortly)

##Branching and merging on GitHub
**Branching** simply means creating a different line of development (branch) where you can test and experiment new things before implementing them into the main codebase. Thus practicing branching will save you from messing up with production codebase.

After testing the new changes on the branch, you can later on integrate them with the main line of development. This act of integrating the branches to the main line of development is known as **merging.**
##Git branch commands
`git branch branchname` command is used to create a new branch
`git checkout  branchname` command is used to move to the specified branch. After checking out to the branch you can now commit and push the changes.
`git checkout-b branchname` command is the short form of the above two commands. It creates a new branch and moves (checks out) to it at the same time.

##Creating remote branches
A locally created branch is only available on your device and not available to the other team members. You can push this local branch to make it available remotely using the below command.


```java
git push –u origin <name>
```


##Deleting branches
`git branch –d branchname` command is used to delete branches after merging the changes.
In some cases git may refuse to delete the branch if it has changes which have not been merged with the main branch which is a safety mechanism to prevent accidental loss of data. 

However, if you are sure you want top delete the branch with uncommitted changes, you can use the command below;


```
git branch –D branchname
```


Also, you use the following command to delete a remote repository;


```
git push origin --delete branchname
```



I greatly appreciate that you stopped by. I hope the article has been of help. You can also check this [article](https://dev.to/ericawanja/how-to-contribute-on-github-2p18/edit) on how to contribute on GitHub and how to sync your repository.




Enter fullscreen mode Exit fullscreen mode
文章来源:https://dev.to/ericawanja/git-and-github-for-beginners-33a0
PREV
在 Spotify 上寻找我最喜欢的新歌曲 设置环境 分析我的播放列表曲目 将我的播放列表与同一类型的样本进行比较 创建一个包含我可能喜欢的歌曲的新播放列表 结论和未来步骤
NEXT
Web 服务器是如何工作的?HTTP 我们做了什么?服务器是如何工作的?服务端点