首页 > 其他分享 >Git 教程:解密 .gitignore 文件、合并分支、解决冲突、及 Git 帮助

Git 教程:解密 .gitignore 文件、合并分支、解决冲突、及 Git 帮助

时间:2024-02-26 21:26:12浏览次数:20  
标签:文件 git -- 解密 Git commit gitignore

Git 帮助

如果你忘记了命令或命令的选项,你可以使用 Git 帮助。

在命令行中,有几种不同的使用帮助命令的方式:

  • git command -help - 查看特定命令的所有可用选项
  • git help --all - 查看所有可能的命令

让我们看看不同的命令。

Git -help 查看特定命令的选项

任何时候,如果你需要帮助来记住特定命令的选项,你可以使用 git command -help

这将显示特定命令的所有可用选项:

usage: git commit [] [--] ...

    -q, --quiet           suppress summary after successful commit
    -v, --verbose         show diff in commit message template

Commit message options
    -F, --file      read message from file
    --author      override author for commit
    --date          override date for commit
    -m, --message 
                          commit message
    -c, --reedit-message 
                          reuse and edit message from specified commit
    -C, --reuse-message 
                          reuse message from specified commit
    --fixup       use autosquash formatted message to fixup specified commit
    --squash      use autosquash formatted message to squash specified commit
    --reset-author        the commit is authored by me now (used with -C/-c/--amend)
    -s, --signoff         add a Signed-off-by trailer
    -t, --template 
                          use specified template file
    -e, --edit            force edit of commit
    --cleanup       how to strip spaces and #comments from message
    --status              include status in commit message template
    -S, --gpg-sign[=]
                          GPG sign commit

Commit contents options
    -a, --all             commit all changed files
    -i, --include         add specified files to index for commit
    --interactive         interactively add files
    -p, --patch           interactively add changes
    -o, --only            commit only specified files
    -n, --no-verify       bypass pre-commit and commit-msg hooks
    --dry-run             show what would be committed
    --short               show status concisely
    --branch              show branch information
    --ahead-behind        compute full ahead/behind values
    --porcelain           machine-readable output
    --long                show status in long format (default)
    -z, --null            terminate entries with NUL
    --amend               amend previous commit
    --no-post-rewrite     bypass post-rewrite hook
    -u, --untracked-files[=]
                          show untracked files, optional modes: all, normal, no. (Default: all)
    --pathspec-from-file 
                          read pathspec from file
    --pathspec-file-nul   with --pathspec-from-file, pathspec elements are separated with NUL character
Note: You can also use --help instead of -help to open the relevant Git manual page

Git help --all 查看所有可能的命令

要列出所有可能的命令,可以使用 help --all 命令:

注意:这将显示一个非常长的命令列表

$ git help --all

这将显示所有可能的命令列表。

合并分支和解决冲突

紧急修复已经准备好,现在让我们合并 master 和 emergency-fix 分支。

首先,我们需要切换到 master 分支:

git checkout master

现在,我们将当前分支(master)与 emergency-fix 合并:

git merge emergency-fix
更新 09f4acd..dfa79db
快进
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

由于 emergency-fix 分支直接来自于 master,并且在我们工作时没有对 master 进行其他更改,Git 将其视为 master 的延续。因此,可以“快进”,将 master 和 emergency-fix 指向相同的提交。

由于 master 和 emergency-fix 现在本质上相同,我们可以删除 emergency-fix,因为它不再需要:

git branch -d emergency-fix
已删除分支 emergency-fix(是 dfa79db)。

合并冲突

现在我们可以切换到 hello-world-images 并继续工作。添加另一个图像文件(img_hello_git.jpg)并更改 index.html,以便显示它:

git checkout hello-world-images
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>

<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<p>A new line in our file!</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>

</body>
</html>

现在,我们已经完成了在该分支上的工作,可以为该分支暂存并提交:

git add --all
git commit -m "added new image"

我们看到 index.html 在两个分支中都发生了更改。现在我们准备将 hello-world-images 合并到 master 中。但是,我们最近在 master 中所做的更改会发生什么?

git checkout master
git merge hello-world-images

自动合并 index.html

合并冲突(内容):index.html 中的合并冲突

自动合并失败;解决冲突,然后提交结果。

合并失败,因为在 index.html 的不同版本之间存在冲突。让我们来检查状态:

git status

在 master 分支上,你有未解决的路径。

(解决冲突并运行 "git commit")

(使用 "git merge --abort" 中止合并)

要提交的更改:

新文件:img\_hello\_git.jpg

新文件:img\_hello\_world.jpg

未解决的路径:

(使用 "git add ..." 标记解决)

两者修改:   index.html

这证实了 index.html 中存在冲突,但图像文件已经准备好并暂存以进行提交。

因此,我们需要解决冲突。在编辑器中打开文件:

<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>

<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<<<<<<< HEAD
<p>This line is here to show how merging works.</p>
=======
<p>A new line in our file!</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>
>>>>>>> hello-world-images

</body>
</html>

我们可以看到不同版本之间的差异,并按照我们的需求进行编辑:

<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
<link rel="stylesheet" href="bluestyle.css">
</head>
<body>

<h1>Hello world!</h1>
<div><img src="img_hello_world.jpg" alt="Hello World from Space" style="width:100%;max-width:960px"></div>
<p>This is the first file in my new Git Repo.</p>
<p>This line is here to show how merging works.</p>
<div><img src="img_hello_git.jpg" alt="Hello Git" style="width:100%;max-width:640px"></div>

</body>
</html>

现在我们可以暂存 index.html 并检查状态:

git add index.html
git status

在 master 分支上,所有冲突都已解决,但你仍在合并中。

(使用 "git commit" 完成合并)

要提交的更改:

新文件:img_hello_git.jpg

新文件:img_hello_world.jpg

修改: index.html

冲突已解决,我们可以使用提交来完成合并:

git commit -m "merged with hello-world-images after fixing conflicts"

然后删除 hello-world-images 分支:

git branch -d hello-world-images
已删除分支 hello-world-images(是 1f1584e)。

现在你对分支和合并的工作方式有了更好的了解。是时候开始与远程仓库一起工作了!

Git .gitignore 文件:创建、示例规则和模式匹配

.gitignore 文件是用于指定 Git 忽略的文件和文件夹的配置文件。这意味着 Git 不会跟踪或包含在版本控制中,但它们仍然存在于你的工作目录中。以下是关于.gitignore文件的详细信息:

创建**.gitignore**文件

要创建一个.gitignore文件,请按照以下步骤操作:

  1. 打开终端或命令行工具。
  2. 导航到你的 Git 存储库的根目录。
  3. 创建.gitignore文件。你可以使用以下命令:touch .gitignore。这将在存储库的根目录中创建一个.gitignore文件。
  4. 使用文本编辑器打开.gitignore文件,你可以添加你要忽略的文件和文件夹的规则。

示例 .gitignore 文件

下面是一个示例.gitignore文件的内容,演示了一些忽略规则:

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

# 忽略任何名为 "temp" 的目录中的所有内容
/temp/

# 忽略所有 .zip 和 .rar 压缩文件
*.zip
*.rar

# 忽略特定文件
config.txt

# 忽略特定文件夹及其内容
bin/
build/

这个.gitignore文件包含了各种忽略规则,例如忽略所有.log文件、名为"temp"的目录、.zip.rar压缩文件、config.txt文件以及bin/build/文件夹及其内容。

.gitignore 文件的规则如下:

  • 模式匹配:.gitignore中的规则使用模式匹配来匹配文件和文件夹。
  • 行注释:以#开头的行将被视为注释。
  • 文件匹配:你可以使用*来匹配任何字符,?来匹配单个字符,[]来匹配字符集,[!...]来否定字符集。
  • 目录匹配:如果模式以/结尾,则该模式仅匹配目录。
  • 递归匹配:使用``来匹配任何子目录。
  • 否定规则:使用!符号来否定已定义的规则。

示例规则包括:

  • *.log:忽略所有扩展名为.log的文件。
  • /temp/:忽略名为"temp"的目录及其内容。
  • bin/:忽略名为"bin"的文件夹及其内容。
  • !important.log:忽略所有.log文件,但不包括名为"important.log"的文件。

通过编辑.gitignore文件,你可以自定义哪些文件和文件夹应该被 Git 忽略,以便它们不会包含在版本控制中。这对于避免将不必要的或敏感文件提交到版本控制中非常有用。

最后

为了方便其他设备和平台的小伙伴观看往期文章:

微信公众号搜索:Let us Coding,关注后即可获取最新文章推送

看完如果觉得有帮助,欢迎 点赞、收藏、关注

标签:文件,git,--,解密,Git,commit,gitignore
From: https://www.cnblogs.com/xiaowange/p/18035393

相关文章

  • Git 帮助手册
    Git帮助手册国外网友制作了一张GitCheatSheet,总结很精炼,各位不妨收藏一下。本节选择性介绍git中比较常用的命令行场景。安装(1)Debian/Ubuntu环境安装如果你使用的系统是Debian/Ubuntu,安装命令为:$apt-getinstalllibcurl4-gnutls-devlibexpat1-devgettext......
  • 终端克隆 GitHub 私有仓库
    在vscode软件内的终端中,克隆自己的私有账户不需要任何验证手段(可能会根据vscode登录的账号进行验证)但是命令行中进行克隆操作,则需要验证:根据官方文档:Aboutremoterepositories-GitHubDocsCloningwithHTTPSURLs需要经过验证,而密码不是自己的账户密码,是personalac......
  • Git基本使用
    Git基本使用Git简介Git是什么Git是一个开源的分布式版本控制系统。Git和其它版本控制系统(包括Subversion和近似工具)的主要差别在于Git对待数据的方式。从概念上来说,其它大部分系统以文件变更列表的方式存储信息,而Git是把数据看作是对小型文件系统的一系列快照。什......
  • Git 第十四阶段:迁移 并更新到最新
    缘起:因为经济寒冬的原因,需要迁移代码到我自己的电脑上。背景环境:Ubuntu迁移操作:1.先删除本地除.git文件夹以外的文件,避免压缩包过大sudorm-rf!(.git*)2.压缩打包sudotar-vzcfAndroid.tgzAndroid3.异地解压缩sudotar-vxfAndroid.tgz-C/home/peng/Desktop/Work/4.变......
  • gogs修改gitclone 仓库地址
      查看gogsdocker容器的名称dockerps进入容器内部dockerexec-itd1bba9f39a02/bin/bash编辑gogs容器的配置文件 vi/data/gogs/conf/app.ini  [server]DOMAIN= gogs.ceshi.online   #这个是ssh的地址  HTTP_PORT=3000EXTERNAL_......
  • 【译】代码更快、更好,借助 GitHub Copilot 的新功能:斜杠命令和上下文变量
    你是否曾经希望有一个人工智能助手可以帮助你更快更好地编写代码?那就是VisualStudioCopilotChat为您提供的:一个人工智能驱动的结对程序员,可以回答您的问题,建议代码片段,解释代码逻辑,并与您讨论您的项目。您可以使用Copilot更快更好地编写代码,因为它可以帮助您避免错误并......
  • goland的git集成不能更新项目
    goland不能拉取,报错;remote:HTTPBasic:Accessdenied.Theprovidedpasswordortokenisincorrectoryouraccounthas2FAenabledandyoumustuseapersonalaccesstokeninsteadofapassword.Seehttp://127.0.0.1:8083/help/topics/git/troubleshooting_git#......
  • git使用指南
    1.基础操作1.1初始化repogitinit1.2添加更改gitadd1.3添加到暂存区gitcommit-m"update"1.4克隆仓库gitclone2.版本管理2.1查看repo状态gitstatus2.2查看文件变化gitdiff2.3查看当前版本的loggitlog2.4查看所有的loggitreflog2.5版本回退g......
  • git中的中文路径显示
    在设置了git控制台编码格式为utf-8后,分别是gitgui工具,commit、log的默认编码:gitconfig--globalgui.encodingutf-8gitconfig--globali18n.commitencodingutf-8gitconfig--globali18n.logoutputencodingutf-8那么在使用gitlog、gitstatus时,会有如下的情况:"M......
  • Git创建版本库及添加远程库
    记录一下如果用Git创建一个本地仓库,在github上创建一个远程仓库,然后让这两个仓库进行远程同步。创建本地版本库(repository)首先找到一个合适的位置,在gitbash中执行$mkdirCodeRepository$cdCodeRepository$pwd/d/CodeRepositorypwd是用来显示当前目录第二步,用初始化......