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

打印 上一主题 下一主题

主题 525|帖子 525|积分 1575

Git 帮助

如果你忘记了命令或命令的选项,你可以使用 Git 帮助。
在命令行中,有几种不同的使用帮助命令的方式:

  • git command -help - 查看特定命令的所有可用选项
  • git help --all - 查看所有可能的命令
让我们看看不同的命令。
Git -help 查看特定命令的选项

任何时候,如果你需要帮助来记住特定命令的选项,你可以使用 git command -help:
这将显示特定命令的所有可用选项:
  1. usage: git commit [] [--] ...
  2.     -q, --quiet           suppress summary after successful commit
  3.     -v, --verbose         show diff in commit message template
  4. Commit message options
  5.     -F, --file      read message from file
  6.     --author      override author for commit
  7.     --date          override date for commit
  8.     -m, --message
  9.                           commit message
  10.     -c, --reedit-message
  11.                           reuse and edit message from specified commit
  12.     -C, --reuse-message
  13.                           reuse message from specified commit
  14.     --fixup       use autosquash formatted message to fixup specified commit
  15.     --squash      use autosquash formatted message to squash specified commit
  16.     --reset-author        the commit is authored by me now (used with -C/-c/--amend)
  17.     -s, --signoff         add a Signed-off-by trailer
  18.     -t, --template
  19.                           use specified template file
  20.     -e, --edit            force edit of commit
  21.     --cleanup       how to strip spaces and #comments from message
  22.     --status              include status in commit message template
  23.     -S, --gpg-sign[=]
  24.                           GPG sign commit
  25. Commit contents options
  26.     -a, --all             commit all changed files
  27.     -i, --include         add specified files to index for commit
  28.     --interactive         interactively add files
  29.     -p, --patch           interactively add changes
  30.     -o, --only            commit only specified files
  31.     -n, --no-verify       bypass pre-commit and commit-msg hooks
  32.     --dry-run             show what would be committed
  33.     --short               show status concisely
  34.     --branch              show branch information
  35.     --ahead-behind        compute full ahead/behind values
  36.     --porcelain           machine-readable output
  37.     --long                show status in long format (default)
  38.     -z, --null            terminate entries with NUL
  39.     --amend               amend previous commit
  40.     --no-post-rewrite     bypass post-rewrite hook
  41.     -u, --untracked-files[=]
  42.                           show untracked files, optional modes: all, normal, no. (Default: all)
  43.     --pathspec-from-file
  44.                           read pathspec from file
  45.     --pathspec-file-nul   with --pathspec-from-file, pathspec elements are separated with NUL character
  46. Note: You can also use --help instead of -help to open the relevant Git manual page
复制代码
Git help --all 查看所有可能的命令

要列出所有可能的命令,可以使用 help --all 命令:
注意:这将显示一个非常长的命令列表
  1. $ git help --all
复制代码
这将显示所有可能的命令列表。
合并分支和解决冲突

紧急修复已经准备好,现在让我们合并 master 和 emergency-fix 分支。
首先,我们需要切换到 master 分支:
git checkout master
现在,我们将当前分支(master)与 emergency-fix 合并:
  1. git merge emergency-fix
复制代码
  1. 更新 09f4acd..dfa79db
  2. 快进
  3. index.html | 2 +-
  4. 1 file changed, 1 insertion(+), 1 deletion(-)
复制代码
由于 emergency-fix 分支直接来自于 master,并且在我们工作时没有对 master 进行其他更改,Git 将其视为 master 的延续。因此,可以“快进”,将 master 和 emergency-fix 指向相同的提交。
由于 master 和 emergency-fix 现在本质上相同,我们可以删除 emergency-fix,因为它不再需要:
  1. git branch -d emergency-fix
复制代码
  1. 已删除分支 emergency-fix(是 dfa79db)。
复制代码
合并冲突

现在我们可以切换到 hello-world-images 并继续工作。添加另一个图像文件(https://www.cnblogs.com/img_hello_git.jpg)并更改 index.html,以便显示它:
  1. git checkout hello-world-images
复制代码
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Hello World!</title>
  5. <link rel="stylesheet" href="bluestyle.css">
  6. </head>
  7. <body>
  8. <h1>Hello world!</h1>
  9. <img src="https://www.cnblogs.com/img_hello_world.jpg" alt="Hello World from Space" >
  10. <p>This is the first file in my new Git Repo.</p>
  11. <p>A new line in our file!</p>
  12. <img src="https://www.cnblogs.com/img_hello_git.jpg" alt="Hello Git" >
  13. </body>
  14. </html>
复制代码
现在,我们已经完成了在该分支上的工作,可以为该分支暂存并提交:
  1. git add --all
  2. git commit -m "added new image"
复制代码
我们看到 index.html 在两个分支中都发生了更改。现在我们准备将 hello-world-images 合并到 master 中。但是,我们最近在 master 中所做的更改会发生什么?
  1. git checkout master
  2. git merge hello-world-images
复制代码
自动合并 index.html
合并冲突(内容):index.html 中的合并冲突
自动合并失败;解决冲突,然后提交结果。
合并失败,因为在 index.html 的不同版本之间存在冲突。让我们来检查状态:
  1. git status
  2. 在 master 分支上,你有未解决的路径。
  3. (解决冲突并运行 "git commit")
  4. (使用 "git merge --abort" 中止合并)
  5. 要提交的更改:
  6. 新文件:img\_hello\_git.jpg
  7. 新文件:img\_hello\_world.jpg
  8. 未解决的路径:
  9. (使用 "git add ..." 标记解决)
  10. 两者修改:   index.html
复制代码
这证实了 index.html 中存在冲突,但图像文件已经准备好并暂存以进行提交。
因此,我们需要解决冲突。在编辑器中打开文件:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Hello World!</title>
  5. <link rel="stylesheet" href="bluestyle.css">
  6. </head>
  7. <body>
  8. <h1>Hello world!</h1>
  9. <img src="https://www.cnblogs.com/img_hello_world.jpg" alt="Hello World from Space" >
  10. <p>This is the first file in my new Git Repo.</p>
  11. <<<<<<< HEAD
  12. <p>This line is here to show how merging works.</p>
  13. =======
  14. <p>A new line in our file!</p>
  15. <img src="https://www.cnblogs.com/img_hello_git.jpg" alt="Hello Git" >
  16. >>>>>>> hello-world-images
  17. </body>
  18. </html>
复制代码
我们可以看到不同版本之间的差异,并按照我们的需求进行编辑:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Hello World!</title>
  5. <link rel="stylesheet" href="bluestyle.css">
  6. </head>
  7. <body>
  8. <h1>Hello world!</h1>
  9. <img src="https://www.cnblogs.com/img_hello_world.jpg" alt="Hello World from Space" >
  10. <p>This is the first file in my new Git Repo.</p>
  11. <p>This line is here to show how merging works.</p>
  12. <img src="https://www.cnblogs.com/img_hello_git.jpg" alt="Hello Git" >
  13. </body>
  14. </html>
复制代码
现在我们可以暂存 index.html 并检查状态:
  1. git add index.html
  2. git status
复制代码
在 master 分支上,所有冲突都已解决,但你仍在合并中。
(使用 "git commit" 完成合并)
要提交的更改:
新文件:https://www.cnblogs.com/img_hello_git.jpg
新文件:https://www.cnblogs.com/img_hello_world.jpg
修改:     index.html
冲突已解决,我们可以使用提交来完成合并:
  1. git commit -m "merged with hello-world-images after fixing conflicts"
复制代码
然后删除 hello-world-images 分支:
  1. git branch -d hello-world-images
复制代码
  1. 已删除分支 hello-world-images(是 1f1584e)。
复制代码
现在你对分支和合并的工作方式有了更好的了解。是时候开始与远程仓库一起工作了!
Git .gitignore 文件:创建、示例规则和模式匹配

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

  • 打开终端或命令行工具。
  • 导航到你的 Git 存储库的根目录。
  • 创建.gitignore文件。你可以使用以下命令:touch .gitignore。这将在存储库的根目录中创建一个.gitignore文件。
  • 使用文本编辑器打开.gitignore文件,你可以添加你要忽略的文件和文件夹的规则。
示例 .gitignore 文件
下面是一个示例.gitignore文件的内容,演示了一些忽略规则:
  1. # 忽略所有 .log 文件
  2. *.log
  3. # 忽略任何名为 "temp" 的目录中的所有内容
  4. /temp/
  5. # 忽略所有 .zip 和 .rar 压缩文件
  6. *.zip
  7. *.rar
  8. # 忽略特定文件
  9. config.txt
  10. # 忽略特定文件夹及其内容
  11. bin/
  12. 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,关注后即可获取最新文章推送
看完如果觉得有帮助,欢迎 点赞、收藏、关注

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

玛卡巴卡的卡巴卡玛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表