Git 技巧

大家好!今天我想和大家分享一些我最近从一本书(学习 Git 和 GitHub)中学到的很酷的 git 技巧,也许这对某些人来说很有趣,对我来说也是自我总结。我将把这篇文章分成三部分,以便获得更好的阅读和写作体验。

在这本书中,我们可以找到许多建议和对 Git 世界中关键概念重要性的提及,例如良好的提交消息、描述性 PR 等等。但在这里,我只想分享一些我学到的有用命令。我处于实习生/初级水平,所以如果这里的主题对你来说很基础,请不要太苛刻地评判我!也许你无论如何都会学到一些东西🤣。所以,让我们继续吧……

Git 配置

`git config` 允许我们个性化 git 安装的一些核心方面。

  • 您可以像这样列出当前配置:
  • # To see all your config
    $ git config --list
    # To see only your global scope config
    $ git congit --global --list
    # To see your local scope config
    $ git config --local --list
    # To see your system config
    $ git config --system --list
    
    # You can add the --show-scope parameter to see the scope of any config
    # only available from 2.26.0 version of git
    $ git config --list --show-scope
    
    # You will see something like this
    # system  diff.astextplain.textconv=astextplain
    # system  filter.lfs.clean=git-lfs clean -- %f
    # system  filter.lfs.smudge=git-lfs smudge -- %f
    # system  filter.lfs.process=git-lfs filter-process
    # system  filter.lfs.required=true
    # system  http.sslbackend=openssl
    # system  http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
    # system  core.autocrlf=true
    # system  core.fscache=true
    # system  core.symlinks=false
    # system  core.editor=nano.exe
    # system  pull.rebase=false
    # system  credential.helper=manager
    # system  credential.https://dev.azure.com.usehttppath=true
    # system  init.defaultbranch=main
    # global  user.email=myMail@gmail.com
    # global  user.name=MyName
    # global  safe.directory=C:/xampp/htdocs
  • 您可以更改一些配置,例如默认文本编辑器……
  • $ git config --global core.editor "code"

    这里我们告诉 Git,我们希望 VS Code 作为全局默认文本编辑器,而不是 nano 编辑器。您可以根据需要指定其他范围或文本编辑器。

  • 另一件有趣的事情是设置全局 gitignore
  • $ git config --global core.excludefile path_to_the_file/.gitignore_global

    Git 签出和 git 切换

    我们经常使用命令在 Git 存储库中创建和/或切换分支。

    没关系,但事实上该命令是一个古老而强大的命令,它可以做更多的事情,而不仅仅是切换分支:

    # Switch to a specific branch
    $ git checkout 
    
    # Create and switch to a new branch
    $ git checkout -b 
    
    # Restore a file or many
    # (Note: if you don't use the -- directive you are creating a new branch...)
    # It will to restore the file to its state in the last commit
    $ git checkout --   ...
    
    # Restore a file to a specific commit state
    $ git checkout  -- 
    
    # Switch to a specific commit
    # Note: If you do this, you won't be on any branch. This state is called "detached HEAD."
    # Any changes you make here can be lost because HEAD is not attached to a branch.
    # To save your changes, you need to create a new branch from this point.
    # In other words, you are not working on any branch at this moment.
    $ git checkout 
    
    # Restore all the working tree
    # It will to restore all to its state in the last commit
    $ git checkout .

    如您所见,该命令可以做很多事情,而且我们可能正在做一些我们实际上不想做的事情……

    为了解决这些潜在的混淆,Git 在 ≥2.23 版本中引入了该命令:

    # Switch to a specific branch
    $ git switch 
    
    # Create and switch to a new branch
    $ git switch -c 

    因此,要将一个或多个文件恢复到上次提交状态,我们可以使用以下命令:

    # Restore une o more files
    $ git restore , 
    # Restore all working tree
    $ git restore .
    # Restore all files with js expension
    $ git restore '*.js'
    # Restore a file to a specific commit state
    $ git restore --source  

    Git 重置

  • 如果你不小心将一个或多个文件添加到存储区域,你可以运行 reset 命令来解决此问题:
  • # Exit a file from the staging area
    $ git reset 
  • Git reset 允许你将分支恢复到特定的提交
  • # Go back to the previous commit and SAVE THE CHANGES 😉
    $ git reset --soft HEAD~1
    
    # Go back to the previous commit and DISCARD ALL the changes
    $ git reset --hard HEAD~1
    
    # Go back to the second commit in this relative position
    $ git reset --hard HEAD~2
    
    # Reset the branch to the remote repo state
    # WARNING: This will eliminate your local commits!
    $ git reset --hard origin/

    当我在本地分支中处理功能或问题时,我会使用这个 `git reset`,我可能会进行多次渐进式提交,但我不希望这些中间提交“扰乱”主分支的提交历史。相反,我的目标是通过一个组织良好的提交来提供干净简洁的 PR。

    这种方法可确保只有一个提交被添加到主分支的历史记录中,从而使团队或负责审查和合并 PR 的人员更容易。

    # Here we see only the commits in my local branch
    $ git log --oneline --no-merges --branches --not --remotes
    
    0f56980 (HEAD -> preprod) add_FTP_to_GA
    1342e47 update_GA
    
    # So, I want only one commit
    # First, we reset to the last commit
    # ⚠️ use --soft !! 😂
    $ git reset --soft HEAD~2
    
    # Then we can do a new sigle commit
    $ git commit -m 'Add FTP integration and update GA configuration'
    
    # And now we have only one commit
    $ git log --oneline --no-merges --branches --not --remotes
    
    e6bc2e1 (HEAD -> preprod) Add FTP integration and update GA configuration

    吉特姆

    想象一下,你添加了一个 `.env` 文件,提交了它,然后意识到你忘记将它添加到 `.gitignore`……也许这并不难想象🤣。

    要解决此问题,您可以使用 `git rm` 命令。虽然 `rm` 本身是一个 shell 命令,但 `git rm` 是 Git 的一部分,它提供了额外的功能来帮助我们管理存储库中的文件

    # Remove the file from the repository but KEEP it locally
    $ git rm --cached .env
    
    # Add it to .gitignore to ensure it's ignored in the future
    $ echo ".env" >> .gitignore
    
    # Commit the changes
    $ git add .gitignore
    $ git commit -m "Remove .env and add it to .gitignore"

    请注意,如果您已将 .env 文件推送到远程存储库,则始终建议您更新敏感密钥,因为 .env 文件位于远程存储库中,这些信息可能已经暴露……

    Git 变基

    当您想让分支与主分支保持同步,而又不引入不必要的合并提交时,“git rebase”命令特别有用。例如,如果您正在处理功能分支(“feature-branch”),而主分支(“main”)已收到更新,则可以使用“git rebase”将您的分支与最新更改同步。这会重新定位您的提交,使它们看起来就像是在“main”中的最新提交之后直接进行的,从而创建更清晰、更线性的历史记录。

    这种方法使提交历史更易于阅读,并避免引入额外的合并提交。它在团队协作时特别有用,因为它可以最大限度地减少潜在冲突并简化将更改合并回主分支的过程。

    # In your feature-branch
    $ git pull --rebase origin main
    # or
    $ git fetch origin
    $ git rebase origin/main

    说明一下情况:

    其中:A---B---C---D'---E'---F'

    没有

    `git rebase` 的另一个用例是,当您想在发布之前修改本地分支的提交历史记录时。需要注意的是,如果您的分支已经发布,则应避免进行此类修改,因为它可能会通过覆盖现有的提交历史记录而产生冲突。您可以将多个提交合并为一个,或者更改提交的消息等等。

    # Here, -i is for interactive rebase
    # HEAD~4 tells Git that we want to rebase the last 4 commits
    $ git rebase -i HEAD~4
    
    # In your text editor, you will see something like this:
    pick  Commit message A
    pick  Commit message B
    pick  Commit message C
    pick  Commit message D
    
    # Now, you can modify the commit history:
    # - 'reword' allows you to change the commit message of one or more commits.
    # - 'squash' tells Git to combine commits into one.
    
    # Example: Changing the message of the second commit and squashing the last two commits into one.
    pick  Commit message A
    reword  Commit message B  # Change the message of this commit
    squash  Commit message C  # Combine this commit with the previous one
    squash  Commit message D  # Combine this commit with the previous one
    
    # After saving and closing the editor:
    # - If you used 'reword', Git will open an editor to change the commit message.
    # - If you used 'squash', Git will prompt you to combine the commit messages.
    
    # This way, you can clean up your commit history, making it more concise and readable before pushing or creating a PR.

    我邀请您查看文档 https://git-scm.com/docs/git-rebase 中的更多详细信息

    结论

    我认为这篇文章就够了。我还有更多东西要分享,但我会留到下一篇文章。我期待您的评论,希望您有所收获!如果您有任何建议或更正,请告诉我!

    感谢您的阅读,下次再见!