**初始化一个新的本地仓库**或**克隆一个已经存在的远程仓库**

本文详细介绍了如何使用Git进行版本控制,包括初始化本地仓库、跟踪文件、提交更改、浏览历史记录及远程仓库的推送与拉取。适合初学者快速上手。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 Git 中,.gitignore 文件是一个非常重要的工具,它帮助开发者忽略掉不需要纳入版本控制的文件或目录(例如生成的日志文件、缓存数据或私密信息)。正确的配置 .gitignore 可以避免无意义的数据污染仓库历史,并提升团队协作效率。下面是详细的说明及步骤:


1. 了解 .gitignore 的作用范围

  • .gitignore 文件的作用仅限于未被跟踪(untracked)的文件和目录。
  • 已经被追踪过的文件不会因为加入到 .gitignore 而移除;若需停止对已追踪文件的管理,应手动删除其索引记录。

2. 编写规则的基本语法

规则示例:
内容含义
*.log匹配所有以“.log”结尾的文件
/temp/完全匹配“/temp/”,即根目录下的 temp 目录
build/忽略 build 目录及其内容
!important.log强制取消忽略 important.log 文件
# 注释行使用井号 (#) 开头表示注释

提示:

  • 空白字符会被忽略。
  • 每一行只能包含一条规则。

3. 创建并应用 .gitignore 文件

方法一:新建空项目时预先设定

如果你正在初始化一个新的Git仓库,在此阶段就可以立即添加一个 .gitignore 文件。

touch .gitignore # 新建.gitignore文件

接着用文本编辑器打开这个文件并将需要屏蔽的内容填写进去保存退出即可生效。

方法二:后期补充给已有工程增加过滤条件

对于已经开始使用的工程项目也是同样操作流程:

echo "*.tmp" >> .gitignore # 添加新规则进现有.gitignore文件里去

注意当向已被提交了多次更改后的旧工程追加新的忽视项之前记得清理残留影响物:

git rm -r --cached .
git add .
git commit -m "更新.gitignore设置"

通过以上命令序列可以把那些已经被错误地标记为受管元素的对象重新标记为非受控状态再依据最新标准处理一遍整个工作区以便同步调整成果反映出来。


4. 常见推荐模板

根据不同语言和技术栈选择适合自己的通用型样版能极大减少重复劳动量同时提高准确率。可以参考官方提供的标准化样式或是访问网上社区贡献集合比如 GitHub/gitignore 找寻对应的预置方案作为起步基础然后再根据实际情形微调优化定制化部分。


获取一个 Git 仓库是开始使用 Git 的第一步。这可以通过两种主要方式实现:初始化一个新的本地仓库克隆一个已经存在的远程仓库。下面详细介绍这两种方法及其相关的命令和注意事项。


方法一:初始化一个新的本地仓库 (Init)

当你有一个尚未受版本控制保护的项目文件夹时,可以将其转换为一个 Git 仓库。以下是步骤:

  1. 进入目标目录
    首先打开终端(Terminal),导航至你想添加版本控制的那个文件夹路径下。

    cd /path/to/your/project
    
  2. 运行 git init 命令
    输入以下命令以初始化新仓库:

    git init
    

    此操作会在该目录内创建一个名为 .git 的隐藏文件夹,里面包含了所有必要的配置信息和数据库结构用于后续的操作如跟踪修改、记录提交等。

  3. 检查状态
    初始化完成后可通过以下指令查看当前的工作树状况:

    git status
    
  4. 首次提交
    将现有文件加入索引区(stage)然后完成第一次commit

    git add .
    git commit -m "Initial commit"
    

注意事项: 如果不想让某些特定类型的文档被列入追踪列表(例如临时编译产物),那么应该事先建立.gitignore文本档来排除它们.


方法二:克隆已有远程仓库 (Clone)

如果需要复制一份来自某个服务器端共享出来的公开资源库或者私人协作型储存空间里的完整资料集到个人机器上来参与编辑维护活动,则需要用到 clone 指令。

基本语法形式如下所示:

git clone <repository-url>

例如要下载 github.com 上面某开源项目的全部源码的话只需指定相应网址即可

git clone https://github.com/user/repo-name.git

上述过程会自动为你执行这几项任务:

  • 创建同名的新文件夹存放提取下来的档案;
  • 设置 origin alias 映射关联原始地址链接方便未来推送变动结果回去原处;
  • 默认检出 master 分支供即时测试体验用途。

另外还有几个常用的选项可供自定义行为模式:

参数描述
–depth N只抓取最新的N层commits的历史纪录加快速度
–bare不包含working directory只保留裸仓

综上所述,无论是想从零构建专属的代码管理环境还是快速接入他人的现成方案,Git都提供了简单直接却又功能齐全的方式满足需求.
Git --local-branching-on-the-cheap

About
Documentation
    Reference
    Book
    Videos
    External Links
Downloads
Community

This book is available in English.

Full translation available in
български език,
Deutsch,
Español,
Français,
Ελληνικά,
日本語,
한국어,
Nederlands,
Русский,
Slovenščina,
Tagalog,
Українська
简体中文,

Partial translations available in
Čeština,
Македонски,
Polski,
Српски,
Ўзбекча,
繁體中文,

Translations started for
azərbaycan dili,
Беларуская,
فارسی,
Indonesian,
Italiano,
Bahasa Melayu,
Português (Brasil),
Português (Portugal),
Svenska,
Türkçe.

The source of this book is hosted on GitHub.
Patches, suggestions and comments are welcome.
Chapters ▾ 2nd Edition
2.1 Git Basics - Getting a Git Repository

If you can read only one chapter to get going with Git, this is it. This chapter covers every basic command you need to do the vast majority of the things you’ll eventually spend your time doing with Git. By the end of the chapter, you should be able to configure and initialize a repository, begin and stop tracking files, and stage and commit changes. We’ll also show you how to set up Git to ignore certain files and file patterns, how to undo mistakes quickly and easily, how to browse the history of your project and view changes between commits, and how to push and pull from remote repositories.
Getting a Git Repository

You typically obtain a Git repository in one of two ways:

You can take a local directory that is currently not under version control, and turn it into a Git repository, or

You can clone an existing Git repository from elsewhere.

In either case, you end up with a Git repository on your local machine, ready for work.
Initializing a Repository in an Existing Directory

If you have a project directory that is currently not under version control and you want to start controlling it with Git, you first need to go to that project’s directory. If you’ve never done this, it looks a little different depending on which system you’re running:

for Linux:

$ cd /home/user/my_project

for macOS:

$ cd /Users/user/my_project

for Windows:

$ cd C:/Users/user/my_project

and type:

$ git init

This creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet. (See Git Internals for more information about exactly what files are contained in the .git directory you just created.)

If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit. You can accomplish that with a few git add commands that specify the files you want to track, followed by a git commit:

$ git add *.c
$ git add LICENSE
$ git commit -m ‘Initial project version’

We’ll go over what these commands do in just a minute. At this point, you have a Git repository with tracked files and an initial commit.
Cloning an Existing Repository

If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to — the command you need is git clone. If you’re familiar with other VCS systems such as Subversion, you’ll notice that the command is “clone” and not “checkout”. This is an important distinction — instead of getting just a working copy, Git receives a full copy of nearly all data that the server has. Every version of every file for the history of the project is pulled down by default when you run git clone. In fact, if your server disk gets corrupted, you can often use nearly any of the clones on any client to set the server back to the state it was in when it was cloned (you may lose some server-side hooks and such, but all the versioned data would be there — see Getting Git on a Server for more details).

You clone a repository with git clone . For example, if you want to clone the Git linkable library called libgit2, you can do so like this:

$ git clone https://github.com/libgit2/libgit2

That creates a directory named libgit2, initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. If you go into the new libgit2 directory that was just created, you’ll see the project files in there, ready to be worked on or used.

If you want to clone the repository into a directory named something other than libgit2, you can specify the new directory name as an additional argument:

$ git clone https://github.com/libgit2/libgit2 mylibgit

That command does the same thing as the previous one, but the target directory is called mylibgit.

Git has a number of different transfer protocols you can use. The previous example uses the https:// protocol, but you may also see git:// or user@server:path/to/repo.git, which uses the SSH transfer protocol. Getting Git on a Server will introduce all of the available options the server can set up to access your Git repository and the pros and cons of each.

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值