侧边栏壁纸
博主头像
LittleAO的学习小站 博主等级

在知识的沙漠寻找绿洲

  • 累计撰写 125 篇文章
  • 累计创建 27 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Git的基础使用

LittleAO
2023-11-28 / 0 评论 / 0 点赞 / 37 阅读 / 0 字
温馨提示:
本文最后更新于2023-11-28,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Git的基础使用指南

安装Git

在官网下载安装合适的版本:https://git-scm.com/
接下来以Windows版本为例。

配置Git

安装完成后,需要配置用户信息:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

创建新仓库或克隆现有仓库

创建新仓库:

git init

克隆现有仓库:

git clone <repository_url>

常用命令

查看文件状态

git status

可能会显示如下输出:

On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

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

        newfile.txt

no changes added to commit (use "git add" and/or "git commit -a")

当前在 master 分支上。
本地 master 分支与远程 origin/master 分支同步。
README.md 文件已被修改,但未暂存。
存在一个未跟踪的新文件 newfile.txt

跟踪新文件或已修改文件

git add <file>
git add .

提交更改

git commit -m "Commit message"

-m后面跟注释。

查看提交历史

git log

可能的输入如下:

commit 1a410efbd13591db07496601ebc7a059dd55cfe9
Author: John Doe <john@example.com>
Date:   Mon Feb 17 21:52:45 2021 -0800

    Fix the bug that caused application crash

commit ca82a6dff817ec66f44342007202690a93763949
Author: Jane Smith <jane@example.com>
Date:   Sun Feb 16 14:35:32 2021 -0800

    Add new feature to the application

commit 085bb3bcb608e1a3a7cf07f4f4c3acb1b7f0fbeb
Author: Alex Johnson <alexj@example.com>
Date:   Sat Feb 15 16:30:38 2021 -0800

    Improve performance of the database query

包括每次提交的以下信息:

  • 提交哈希,这是每个提交的唯一标识符,通常是一个40位的SHA-1哈希值。
  • 作者,提交者的姓名和电子邮件地址。
  • 日期。
  • 注释。

git log 命令还有许多选项可以定制输出的信息,例如:

git log -n <limit>:显示最近的 <limit> 次提交。
git log --oneline:将每个提交压缩到一行显示。
git log --graph:以ASCII图形显示分支和合并历史。
git log --author="<pattern>":只显示特定作者的提交。
git log --since=2.weeks:只显示最近两周的提交。

创建分支

git branch <branch_name>

切换分支

git checkout <branch_name>

合并分支

git merge <branch_name>

拉取远程仓库更新到本地

git merge <branch_name>

推送本地更新到远程仓库

git push

分支管理

分支是独立进行更改的工作副本。通过分支,可以隔离开发工作,不影响其他分支。

查看所有分支

git branch -a

可能的输出如下:

  master
  feature/login
* feature/signup
  remotes/origin/master
  remotes/origin/feature/login
  remotes/origin/feature/payment

在这个例子中:

  • masterfeature/login 是本地分支。
  • feature/signup 是当前检出的分支,前面有一个 * 标记。
  • remotes/origin/masterremotes/origin/feature/loginremotes/origin/feature/payment 是远程跟踪分支,指向 origin 远程仓库的分支。

删除分支

git branch -d <branch_name>

撤销更改

撤销工作目录中的更改

git checkout -- <file>

撤销暂存的文件

git reset HEAD <file>

撤销提交

git revert <commit>

重写提交历史(慎用)

git rebase

处理冲突

当多个分支编辑了同一个文件的同一部分时,合并这些分支可能会导致冲突。解决冲突通常需要手动编辑文件,并重新提交。

使用 .gitignore 文件

创建 .gitignore 文件可以让 Git 忽略不需要纳入版本控制的文件和目录。
.gitignore 文件中,你可以添加规则来指定哪些文件和目录应该被忽略。每行一个规则,例如:

# 忽略所有 .log 文件
*.log

# 忽略特定目录
tmp/

# 忽略特定文件
config/database.yml

# 忽略除某文件外的所有文件
!keepme.txt

# 忽略所有在 .env 文件夹中的 .json 文件
.env/*.json

# 忽略除 .env/development.json 之外的所有 .env 中的 .json 文件
!.env/development.json

一旦添加了想要忽略的文件和目录,你需要将 .gitignore 文件提交到你的 Git 仓库中:

git add .gitignore
git commit -m "Add .gitignore file"
git push

如果你想要查看哪些文件被 .gitignore 忽略,你可以使用以下命令:

git status --ignored

请注意,如果你之前已经提交了一些现在想要忽略的文件,仅仅是添加这些文件到 .gitignore 是不够的。Git 仍会跟踪这些文件,因为它们已经在仓库的历史中。要从 Git 仓库中移除这些文件,你需要使用 git rm --cached 命令将它们从索引中移除(但保留在你的本地文件系统中):

git rm --cached FILENAME

远程仓库

远程仓库通常托管在服务如 GitHub、GitLab 或 Gitee 上。

添加远程仓库

git remote add <shortname> <url>

查看远程仓库

git remote -v

删除远程仓库

git remote rm <shortname>
0

评论区