- 1. 实验内容及目的 Contents & Objectives for Lab
- 2. Git 的简要介绍 Brief Introduction for Git
- 3. Git的安装 Installation for Git
- 4. Git的一些基本软件命令操作介绍 Basic Operation Commands for Git
- 4.1 克隆已有仓库 Clone Existing Repository
- 4.2 打开现有项目 Open Existing Projects
- 4.2.1 **进入项目目录 ** Entering the Project Directory
- 4.2.2 初始化Git仓库 Initializing a Git Repository
- 4.2.3 添加文件到版本控制 Adding Files to Version Control
- 4.2.4 **提交初次版本 ** Committing the Initial Version
- 4.2.5 **连接远程仓库 ** Connecting to a Remote Repository
- 5. 操作中的问题与解决 Problems & Solutions
- 6. 实验总结思考点
- 6.1 Git的实用性
- 6.2 Git在项目中的作用
- 6.3 Git学习和实践中的挑战
- 6.4 总结与展望
- 7.学习参考资料 Reference Materials for Learning
- 7.1 官方文档 Official Documentation
- 7.2 视频课程 Video Courses
1. 实验内容及目的 Contents & Objectives for Lab
a. 了解Git软件的作用,以及其在多人项目管理中所起到的重要作用
b. 学会并掌握Git软件的安装以及配置工作
c. 掌握一些基本的Git命令的操作,例如如何从如GitHub这类的网站上拉取项目,以及如何自己通过Git来创建一个多人管理项目并更新
a. Understand the role of Git software and its importance in managing collaborative projects.
b. Learn and master the installation and configuration of Git software.
c. Master some basic Git commands, such as how to pull projects from websites like GitHub and how to create and update a collaborative project using Git.
2. Git 的简要介绍 Brief Introduction for Git
Git是一个源码开放的分布式版本控制软件系统,用户使用它能够更加有效且高速地进行不同大小的版本项目管理。其最初由最初由林纳斯·托瓦兹创作,于2005年以GPL许可协议发布。
在主要功能上,git是用于Linux内核开发的版本控制工具。与CVS、Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法,不需要服务器端软件,就可以运作版本控制,使得源代码的发布和交流极其方便。git的速度很快,这对于诸如Linux内核这样的大项目来说自然很重要。git最为出色的是它的合并追踪(merge tracing)能力。
实际上Linux内核开发团队决定开始开发和使用git来作为内核开发的版本控制系统的时候,世界上开源社群的反对声音不少,最大的理由是git太艰涩难懂,从git的内部工作机制来说,的确是这样。但是随着开发的深入,git的正常使用都由一些友善的命令来执行,使git变得非常好用。现在,越来越多的著名项目采用git来管理项目开发,例如:wine、U-boot等。
作为开源自由原教旨主义项目,git没有对版本库的浏览和修改做任何的权限限制,通过其他工具也可以达到有限的权限控制,比如:gitosis、CodeBeamer MR。原本git的使用范围只适用于Linux/Unix平台,但在Windows平台下的使用也日渐成熟,这主要归功于Cygwin、msysgit环境,以及TortoiseGit这样易用的GUI工具。git的源代码中也已经加入了对Cygwin与MinGW编译环境的支持且逐渐完善,为Windows用户带来福音。
同样地,在实现原理上,git和其他版本控制系统(如CVS)有不小的差别,git本身关心文件的整体性是否有改变,但多数的版本控制系统如CVS或Subversion系统则在乎文件内容的差异。git拒绝保持每个文件的版本修订关系。因此查看一个文件的历史需要遍历各个history快照;git隐式处理文件更名,即同名文件默认为其前身,如果没有同名文件则在前一个版本中搜索具有类似内容的文件。
git更像一个文件系统,直接在本地上获取资料,不必连线到主机端获取资料。 每个开发者都可有全部开发历史的本地副本,changes从这种本地repository复制给其他开发者。这些changes作为新增的开发分支被导入,可以与本地开发分支合并。
Git is an open-source distributed version control software system that allows users to manage projects of various sizes more efficiently and quickly. It was originally created by Linus Torvalds and released in 2005 under the GPL license.
In terms of functionality, Git is a version control tool used for Linux kernel development. Unlike centralized version control tools such as CVS and Subversion, Git uses a distributed repository approach, which means it can operate without server-side software, making source code distribution and collaboration extremely convenient. Git is also very fast, which is important for large projects like the Linux kernel. One of Git's most outstanding features is its merge tracking capability.
When the Linux kernel development team decided to start developing and using Git as the version control system for kernel development, there was a lot of opposition from the open-source community, with the main reason being that Git was too complex and difficult to understand. From Git's internal working mechanism, this criticism was indeed valid. However, as development progressed, Git's usage was facilitated by friendly commands, making it much more user-friendly. Nowadays, many well-known projects, such as Wine and U-Boot, use Git to manage their development.
As a project rooted in open-source freedom principles, Git does not impose any permission restrictions on repository browsing and modifications. However, limited permission control can be achieved through other tools such as gitosis and CodeBeamer MR. Originally, Git was only used on Linux/Unix platforms, but its usage on Windows has also become increasingly mature, thanks to environments like Cygwin, msysgit, and user-friendly GUI tools like TortoiseGit. Git's source code now includes support for Cygwin and MinGW compilation environments, which have gradually improved, bringing benefits to Windows users.
Similarly, in terms of implementation, Git differs significantly from other version control systems such as CVS. Git focuses on whether the integrity of a file has changed, while most version control systems, like CVS or Subversion, are concerned with the differences in file content. Git does not maintain a version history for each file. Therefore, viewing the history of a file requires traversing each history snapshot; Git implicitly handles file renaming by assuming that a file with the same name is its predecessor. If there is no file with the same name, it searches for files with similar content in the previous version.
Git functions more like a file system, retrieving data locally without needing to connect to a remote server. Each developer can have a complete local copy of the development history, and changes can be copied from this local repository to other developers. These changes are imported as new development branches, which can then be merged with local development branches.
3. Git的安装 Installation for Git
注意:由于本人的电脑已经安装有Git,故为演示操作,本安装操作均在 WIndows 8 虚拟机系统下进行
首先,我们登录Git的官方网站,并且通过其 下载页面以下载对应系统版本的Git软件。在这里,我们以Windows系统为例。
Note: Since Git is already installed on my computer, for demonstration purposes, this installation will be performed on a Windows 8 virtual machine.
First, we log in to Git's official website and download the appropriate version of Git for your system through its download page. Here, we will take the Windows system as an example.
点击Windows版本下载,并且以管理员身份运行对应的 exe 文件,开始会展示对应的Info信息,点击Next,来到选择对应安装组件的界面(如下图所示),在这里我们可以选择如是否添加图标、是否在右键添加Git操作选项以及是否与对应文件相关联、检查更新等操作。我们按照自己的需求勾选对应选项,点击Next进入下一步。
Click to download the Windows version, and run the corresponding exe file as an administrator. The installation will begin by displaying the relevant Info information. Click "Next" to proceed to the component selection screen (as shown in the image below). Here, you can choose options such as adding icons, adding Git options to the right-click context menu, associating Git with corresponding files, and checking for updates. Select the options according to your needs, then click "Next" to proceed to the next step.
接下来需要我们注意的便是对于默认Editor的选择问题,Vim编辑器由于历史遗留以及不够直观等问题,已经不再推荐使用,这里使用Nano Editor进行安装:
Next, we need to pay attention to the selection of the default editor. Due to historical issues and lack of intuitiveness, the Vim editor is no longer recommended. Here, we will select Nano Editor for installation:
接下来的操作可以按照默认进行即可,一路点击继续,我们看到绿色的安装进度条以及最终的安装成功提示界面后,便证明我们已经成功在我们的系统中安装好了Git版本控制软件。
The subsequent operations can be performed with the default settings. Simply continue clicking "Next." Once you see the green installation progress bar and the final installation success screen, it confirms that Git version control software has been successfully installed on your system.
4. Git的一些基本软件命令操作介绍 Basic Operation Commands for Git
对于Git这个软件,我们可以主要用来进行仓库项目的克隆、推送、创建以及打开对应的本地项目。接下来我们就以简单的几个例子来介绍我们的操作流程。
With Git, we can primarily use it to clone, push, create repositories, and open corresponding local projects. Next, we will introduce our workflow through a few simple examples.
4.1 克隆已有仓库 Clone Existing Repository
当我们在GitHub上看到我们感兴趣的开源项目并且想将其复制到本地来进行下一步的研究时,我们通常会用到Git软件的Clone命令来将项目复制到本地,一般而言,Clone命令的主要组织形式大致如下:
When we see an open source project on GitHub that we are interested in and want to copy it locally for the next step in our research, we usually use the Git software's Clone command to copy the project locally, and in general, the main organization of the Clone command is roughly as follows:
git clone GIT_URL YOUR_DESTINATION
其中,GIT_URL为网站Repository中所给的连接地址,而YOUR_DESTINATION则为项目所克隆到的本地目标位置。以我在GitHub上的一个项目为例,在网站上,我们可以看到对应的克隆地址,我想将其拷贝到本地的桌面中,因此打开Git的CMD命令行窗口,输入对应的命令,得到下图7所示的结果,即代表项目已经成功克隆到对应的路径下:
Where GIT_URL is the connection address given in the website Repository, and YOUR_DESTINATION is the local target location that the project is cloned to. Take one of my projects on GitHub as an example, on the website, we can see the corresponding clone address, I want to copy it to the local desktop, so open the CMD command line window of Git, enter the corresponding commands, and get the results shown in Figure 7 below, that is, it means that the project has been successfully cloned to the corresponding path:
4.2 打开现有项目 Open Existing Projects
打开本地的现有项目并且将其加入版本控制主要分为以下几个步骤:首先是进入项目目录,而后我们需要初始化Git仓库,而后再将文件添加到版本控制。
There are several steps to open an existing local project and add it to version control: first, you need to enter the project directory, then you need to initialize the Git repository, and then you need to add the files to version control.
4.2.1 **进入项目目录 ** Entering the Project Directory
打开终端或命令提示符,并导航到你想要使用Git进行版本控制的本地项目目录。例如:
cd "C:\Users\Jerry Brown\Desktop\Your_Project"
如果目录路径中有空格,请记得使用双引号包裹路径。
4.2.2 初始化Git仓库 Initializing a Git Repository
进入项目目录后,运行以下命令来初始化一个新的Git仓库:
Once in the project directory, run the following command to initialize a new Git repository:
git init
这个命令会在当前目录中创建一个隐藏的 .git
文件夹,Git会在这个文件夹中保存与版本控制相关的信息。
This command will create a hidden .git
folder in the current directory, where Git will store information related to version control.
4.2.3 添加文件到版本控制 Adding Files to Version Control
运行以下命令,将当前项目中的所有文件添加到Git的暂存区:
Run the following command to add all files in the current project to Git’s staging area:
git add .
这里的 .
表示将当前目录中的所有文件和文件夹添加到暂存区。你也可以指定某个文件或文件夹来添加。
The .
indicates that all files and folders in the current directory should be added to the staging area. You can also specify individual files or folders to add.
4.2.4 **提交初次版本 ** Committing the Initial Version
添加文件到暂存区后,可以使用以下命令提交第一个版本:
After adding files to the staging area, you can use the following command to commit the first version:
git commit -m "Initial commit"
-m "Initial commit"
用来添加提交时的备注信息,这条信息描述了这次提交的内容。
The -m "Initial commit"
option adds a message to the commit, describing the content of this commit.
4.2.5 **连接远程仓库 ** Connecting to a Remote Repository
如果我们有远程仓库(例如在GitHub或GitLab上),可以使用以下命令将本地仓库与远程仓库关联:
If you have a remote repository (e.g., on GitHub or GitLab), you can use the following command to link the local repository to the remote one:
git remote add origin https://github.com/YourUsername/YourRepository.git
然后推送代码到远程仓库:
Then push the code to the remote repository:
git push -u origin master
这样,我们就可以在本地项目中使用Git进行版本控制,并且可以选择将其推送到远程仓库中。
In this way, you can use Git for version control in your local project and optionally push it to a remote repository.
5. 操作中的问题与解决 Problems & Solutions
在Git clone中,我们可能会遇到如下connection error问题:
In Git clone, we might face the connection error problem:
在确保网络连接正常之后,我们可以通过分别输入以下命令行解决:
First to make sure that the Internet connection is working, we can solve this problem by inputing these two lines of codes respectively:
git config --global http.proxy socks5 127.0.0.1:7890
git config --global https.proxy socks5 127.0.0.1:7890
6. 实验总结思考点
在总结Git的安装与实践操作时,可以从以下几个方面进行思考和总结:
6.1 Git的实用性
-
版本控制的核心工具:Git作为一个分布式版本控制系统,能够有效管理项目中的代码版本。通过Git,开发者可以轻松地追踪代码的历史变化,快速回滚到早期版本,并在团队协作中减少冲突。
Core Tool for Version Control: As a distributed version control system, Git effectively manages code versions in a project. With Git, developers can easily track changes in code history, quickly revert to earlier versions, and reduce conflicts in team collaboration. -
提高开发效率:在多人协作开发中,Git提供了分支功能,使得开发者可以在独立的分支上进行开发,而不会影响主线代码。这种并行开发的方式显著提升了开发效率,并减少了开发过程中出错的风险。
Improving Development Efficiency: In collaborative development, Git provides branching functionality, allowing developers to work on separate branches without affecting the mainline code. This parallel development approach significantly enhances development efficiency and reduces the risk of errors during development.
6.2 Git在项目中的作用
-
分布式特点:Git的分布式架构意味着每个开发者都拥有整个项目的完整历史记录,这不仅提高了项目的安全性(防止数据丢失),还增强了开发者的灵活性,即使没有网络连接也能进行操作。
Distributed Nature: Git's distributed architecture means that every developer has a complete history of the entire project, which not only increases project security (preventing data loss) but also enhances developer flexibility, allowing operations even without network connectivity. -
强大的分支管理:Git的分支机制非常强大和灵活,允许开发者创建、合并和删除分支。通过分支,开发者可以轻松实现不同功能的开发、测试、上线,并在必要时进行分支合并,从而确保代码的稳定性。
Powerful Branch Management: Git's branching mechanism is very powerful and flexible, allowing developers to create, merge, and delete branches. With branching, developers can easily develop, test, and deploy different features, and merge branches when necessary to ensure code stability. -
简化协作流程:通过Git,团队可以通过Pull Request或Merge Request等方式进行代码审查,保证代码质量。此外,Git的分布式特性也方便团队在不同地点进行协作开发,避免了中央服务器的单点故障问题。
Simplified Collaboration: With Git, teams can conduct code reviews through Pull Requests or Merge Requests to ensure code quality. Additionally, Git's distributed nature facilitates collaborative development from different locations, avoiding the single point of failure issue of a central server.
6.3 Git学习和实践中的挑战
-
命令行工具的学习:虽然Git的命令行工具功能强大,但对于初学者来说,命令的复杂性可能会带来一定的学习曲线。需要通过不断实践和查阅文档,逐步掌握常用命令的使用。
Learning Command-Line Tools: Although Git's command-line tools are powerful, their complexity can present a learning curve for beginners. Continuous practice and consultation of documentation are required to gradually master the use of common commands. -
版本冲突的处理:在多人协作时,版本冲突不可避免,如何有效处理冲突、合理规划分支策略也是实践中需要重点掌握的内容。
Handling Version Conflicts: In collaborative environments, version conflicts are inevitable. Effectively handling conflicts and planning branch strategies are key aspects that need to be mastered in practice.
6.4 总结与展望
-
实用性和普及性:Git作为目前最流行的版本控制工具,其应用范围已经超出了软件开发领域,广泛用于文档管理、数据分析、研究项目等各个领域。掌握Git不仅是开发者的基本技能,也对提升项目管理和团队协作能力有显著帮助。
Practicality and Popularity: As the most popular version control tool today, Git's application extends beyond software development to document management, data analysis, research projects, and more. Mastering Git is not only a fundamental skill for developers but also significantly enhances project management and team collaboration abilities. -
未来应用的思考:随着DevOps、CI/CD等现代开发流程的普及,Git在自动化部署、持续集成中的作用越来越突出。未来在使用Git时,可以更深入地结合这些工具,提高项目开发和发布的效率。
Future Applications: With the rise of modern development processes like DevOps and CI/CD, Git's role in automated deployment and continuous integration is becoming more prominent. In the future, using Git in conjunction with these tools can further enhance project development and release efficiency.
相信通过对Git的实践操作,不仅加深了我们对其核心概念的理解,同时也为我们今后的项目开发提供了强有力的工具支持。
By practicing Git, we have deepened our understanding of its core concepts and provided strong tool support for our future project development.
7.学习参考资料 Reference Materials for Learning
以下是一些学习Git的参考资料,包括文档、书籍、在线课程和社区资源,适合我们不同水平的学习者以供学习:
Below are some references for learning Git, including documentation, books, online courses, and community resources, suitable for learners at different levels:
7.1 官方文档 Official Documentation
-
Git 官方文档 (英文)
Git官网提供的文档非常详尽,涵盖了从入门到高级使用的各个方面。适合需要深入理解Git命令和工作原理的学习者。
Git Official Documentation (English)
The documentation provided by Git's official website is very detailed, covering all aspects from beginner to advanced usage. It is suitable for learners who need a deep understanding of Git commands and working principles. -
Pro Git (中文翻译)
这本书是Git的官方推荐教材,内容全面且易于理解,适合系统性学习Git。
Pro Git (Chinese Translation)
This book is an officially recommended textbook by Git. It is comprehensive and easy to understand, making it suitable for systematic learning of Git.
7.2 视频课程 Video Courses
- Coursera 上的 Version Control with Git
Coursera的Git课程,由知名大学和机构提供,课程质量较高,并且可以获得认证。
Version Control with Git on Coursera
The Git course on Coursera, provided by well-known universities and institutions, offers high-quality content and certification options.