git add 和git

    科技2026-08-08  17

    git add 和git

    .gitkeep,GIT跳转,GIT日志,GIT存放,差异化(.gitkeep, GIT Jump, GIT Log, GIT Stash, diff-so-fancy)

    The Git system is a powerful tool for tracking source code changes. Commonly used by developers and programmers across the globe, gaining a familiarity with this technology is a great asset to have. This article covers a number of tips and tricks to help improve your Git literacy and workflow.

    Git系统是跟踪源代码更改的强大工具。 全球开发人员和程序员普遍使用该技术,这是一笔宝贵的财富。 本文介绍了许多技巧和窍门,以帮助您提高Git的素养和工作流程。

    #1使用.gitkeep提交并推送空的Git文件夹 (#1 Use .gitkeep to commit and push empty Git folders)

    Git doesn’t like to have empty folders. It doesn’t include them in the commits and it certainly doesn’t include empty folders when you push to GitHub or GitLab. But sometimes that causes a problem, which is where the .gitkeep file comes in.

    Git不喜欢有空文件夹。 当您推送到GitHub或GitLab时,它不包含在提交中,当然也不包含空文件夹。 但是有时这会导致问题,这是.gitkeep文件进入的地方。

    使用.gitkeep文件的步骤 (Steps to use the .gitkeep file)

    To use the .gitkeep file to have Git push empty directories, follow these steps:

    要使用.gitkeep文件使Git推送空目录,请按照下列步骤操作:

    Create a folder with the mkdir command

    使用mkdir命令创建一个文件夹 Move into the empty folder with the cd command

    使用cd命令移至空白文件夹Create the .gitkeep file with the touch .gitkeep command

    使用touch .gitkeep命令创建.gitkeep文件Use git add . to update the Git index

    使用git add。 更新Git索引 Perform a Git commit

    执行Git提交Push the empty Git folder to remote Git repositories such as GitHub or GitLab

    将空的Git文件夹推送到远程Git存储库,例如GitHub或GitLab

    That’s how easy it is to make Git include empty directories in a commit or push.

    这就是让Git在提交或推送中包含空目录的过程如此简单。

    gitkeep@example:~$ mkdir empty-directorygitkeep@example:~$ cd empty-directorygitkeep@example:~$ touch .gitkeepgitkeep@example:~$ git add .gitkeep@example:~$ git commit -m "Commit empty folder in Git with gitkeep"gitkeep@example:~$ git push origin

    It’s worth noting that — unlike the .gitignore file or the .git folder — the .gitkeep file is not part of the Git tool, the Git standard, or any Git API. It’s kind of like the hidden menu at In-N-Out Burger. For regular users of the tool, the use of the .gitkeep file is well known, but it’s certainly not part of the official documentation. Maybe on some future release, the .gitkeep file will be officially supported, but for now, there is nothing particularly special about it.

    这是值得一提的是-不像的.gitignore文件或文件夹git的-该.gitkeep文件不是Git的工具,Git的标准,或任何Git的API的一部分。 这有点像In-N-Out Burger中的隐藏菜单。 对于该工具的普通用户来说,.gitkeep文件的使用是众所周知的,但是它当然不是官方文档的一部分。 可能在将来的某些发行版中,将正式支持.gitkeep文件,但是到目前为止,没有什么特别的。

    演示地址

    Cameron McKenzie Cameron McKenzie

    #2 GIT隐藏合并冲突 (#2 GIT stash merge conflict)

    There’s always the chance that a merge conflict might occur when a developer updates or overwrites a file. Here are the steps to resolve a Git stash merge conflict.

    当开发人员更新或覆盖文件时,总是有可能发生合并冲突。 以下是解决Git藏匿合并冲突的步骤。

    First, initialize a Git repository, create a file, and add it to the index. A commit with the message “First commit” will occur.

    首先,初始化一个Git存储库,创建一个文件,并将其添加到索引中。 将会出现带有“首次提交”消息的提交。

    $ git init$ touch stash-pop-conflict.html$ git add .$ git commit -m "First commit"

    The file will then be edited, stashed, edited again, and then made part of a commit. At the end of this following sequence of steps, the file in the stash is out of sync with the committed file in the workspace. This will cause a Git stash pop conflict.

    然后将对该文件进行编辑,隐藏,再次编辑,然后成为提交的一部分。 在以下步骤序列的最后,存储区中的文件与工作空间中已提交的文件不同步。 这将导致Git隐藏弹出冲突。

    $ echo "Hello World" >> stash-pop-conflict.html$ git stash$ echo "Goodbye World" >> stash-pop-conflict.html$ git commit -a -m "Work Done!"

    Now, the Git stash pop command causes a problem.

    现在,Git stash pop命令导致出现问题。

    $ git stash popAuto-merging stash-pop-conflict.htmlCONFLICT (content): Merge conflict in stash-pop-conflict.htmlThe stash entry is kept in case you need it again.

    Manually update the file; in this example, we used the echo command to perform the update and resolve the issue. After the Git stash merge conflict is resolved, add the file to the index and commit the change.

    手动更新文件; 在此示例中,我们使用echo命令执行更新并解决问题。 解决Git藏匿合并冲突后,将文件添加到索引并提交更改。

    $ echo "This will resolve conflicts." >> stash-pop-conflict.html$ git add .$ git commit -m "all merged"[master 06d8404] all merged1 file changed, 1 insertion(+), 1 deletion(-)

    The Git stash pop command is supposed to delete the latest record in the stash history, but the Git stash list command shows that this hasn’t happened, which is to be expected with a failed pop. Now perform a Git stash drop command manually.

    应该使用Git stash pop命令删除存储历史记录中的最新记录,但是Git stash list命令显示这没有发生,这是由于弹出失败而导致的。 现在,手动执行Git stash drop命令。

    $ git stash liststash@{0}: WIP on master: ffb2b5a First commit$ git stash drop

    A reflog inspection shows two commit messages and a reset.

    检查日志显示两个提交消息和一个重置。

    $ git reflog06d8404 (HEAD -> master) HEAD@{0}: commit: all merged718f4b0 HEAD@{1}: commit: Work Done!ffb2b5a HEAD@{2}: reset: moving to HEADffb2b5a HEAD@{3}: commit (initial): First commit

    That is how to resolve a Git stash pop conflict and restore the workspace to the state it would have been in if the conflict never happened in the first place.

    这就是解决Git隐藏弹出冲突并将工作空间恢复到如果冲突从未发生过时原本应处于的状态的方法。

    #3 GIT推送存储未跟踪的文件 (#3 GIT stash untracked files with a push)

    By default, the Git stash push and save commands will only save files that were either added to the Git index or are part of the Git commit message history. However, it is possible to alter this behavior and stash untracked files with the right git stash save and push options.

    默认情况下,Git藏匿推入和保存命令将仅保存添加到Git索引中或属于Git提交消息历史记录的文件。 但是,可以更改此行为,并使用正确的git stash save和push选项存储未跟踪的文件。

    The trick is to use the — include-untracked option or for brevity, the -u alias.

    诀窍是使用-include-untracked选项,或者为了简洁起见,使用-u别名。

    A way to further alter that standard git stash behavior is to use the — all option or its corresponding -a alias. The caveat with the — all switch is that it will not only include untracked files in the stash, but it will also capture files otherwise excluded by the .gitignore file.

    进一步更改标准git藏匿行为的一种方法是使用-all选项或其对应的-a别名。 使用-all开关的警告是,它不仅将在存储中包含未跟踪的文件,而且还将捕获.gitignore文件所排除的文件。

    With both the — include-untracked and — all switches, the word ‘push’ can be left out of the command because it’s the assumed default.

    使用– include-untracked和–所有开关,可以将“ push”一词排除在命令之外,因为它是默认设置。

    $ git stash — all$ git stash -a

    #4比较git stash pop和git stash申请文件还原 (#4 Compare git stash pop and git stash apply for file restores)

    When we pull a stored snapshot from the Git stash, they can use either the pop or apply option. But which one should you choose? The choice between Git stash pop versus apply really boils down to how long you want to keep the banked changes.

    当我们从Git存储库中提取存储的快照时,他们可以使用pop或apply选项。 但是,您应该选择哪一个呢? Git隐藏流行与应用之间的选择实际上归结为您要保留存储更改的时间。

    The key difference between git stash pop and apply involves the stash history.

    git stash pop和apply之间的主要区别在于存储历史。

    In more technical terms, the Git stash pop command is a combination of Git stash apply and Git stash drop. If the apply and drop commands are invoked separately, the result is identical if the Git stash pop command was invoked.

    用更专业的术语来说,Git stash pop命令是Git stash apply和Git stash drop的组合。 如果分别调用apply和drop命令,则调用Git stash pop命令的结果是相同的。

    无冲突git stash pop (No-conflict git stash pop)

    Initialize a Git repository

    初始化一个Git仓库 Add a file

    新增档案Perform a commit

    执行一次提交Edit the file

    编辑档案Stash it and

    藏起来Use the Git stash pop command to restore the stashed file.

    使用Git stash pop命令还原保存的文件。

    Notice that the git stash list command issued after the pop indicates that there is no stash history. The popped stash has been deleted.

    请注意,在弹出窗口之后发出的git stash list命令表明没有存储历史记录。 弹出的存储区已被删除。

    $ git init$ echo “git stash pop and apply” >> stash-pop.html$ git add .$ git commit -m “git stash pop example”$ echo “no git stash pop conflict” >> stash-pop.html$ git stash$ git stash liststash@{0}: WIP master: a6d5913 git stash pop example$ git stash pop$ git stash listno git stash history to show$ git refloga6d5913 HEAD@{0}: reset: moving to HEADa6d5913 HEAD@{1}: commit: git stash pop example

    没有冲突的git stash适用 (No-conflict git stash apply)

    This Git stash apply example follows the exact same path as the pop example above, but when the stash history is listed the applied stash is still there. This is the fundamental difference between the two commands.

    这个Git隐藏应用示例遵循与上面的pop示例完全相同的路径,但是当列出存储历史时,所应用的存储仍然存在。 这是两个命令之间的根本区别。

    $ git init$ echo “git stash apply and pop” >> stash-apply.html$ git add .$ git commit -m “git stash apply example”$ echo “no git stash apply conflict” >> stash-apply.html$ git stash$ git stash liststash@{0}: WIP master: c112505 git stash apply example$ git stash apply$ git stash liststash@{0}: WIP master: c112505 git stash apply example

    Git隐藏流行并应用最佳实践 (Git stash pop and apply best practices)

    We shouldn’t shelve Git changes in the stash too often. Use the stash for temporary, short-term storage of file system changes. If changes must be stored over a prolonged period, commit those changes to a topic branch.

    我们不应该过多地搁置Git的更改。 将存储空间用于文件系统更改的临时,短期存储。 如果必须长时间存储更改,请将这些更改提交到主题分支。

    As such, the idea of keeping a local stash around for an extended time runs counter to the basic philosophy of distributed version control with Git. That’s why it’s recommended to use Git stash pop to restore conflict-free files instead of the complementary Git stash apply command.

    因此,长时间保留本地存储的想法与Git的分布式版本控制的基本原理背道而驰。 这就是为什么建议使用Git stash pop还原无冲突文件而不是补充的Git stash apply命令的原因。

    #5使用git log graph命令 (#5 Using the git log graph command)

    The coolest features of the Git log command are the graphing feature. The Git log graph command creates a graphic overview of how your various development pipelines have branched and merged over time. The fact that this visual display is done through a decidedly non-visual BASH shell or terminal window is what makes the feat particularly impressive.

    Git log命令最酷的功能是绘图功能。 Git日志图命令创建一个图形化的概述,说明随着时间的推移,您的各种开发管道如何分支和合并。 这种视觉显示是通过非视觉的BASH外壳或终端窗口完成的,这一壮举尤其令人印象深刻。

    git log graph命令 (git log graph command)

    The simplest form of the Git log graph command looks as follows:

    Git日志图命令的最简单形式如下所示:

    $ git log -–graph

    The problem with the basic –graph usage is that the verbosity of the log gets in the way. Matching the Git log graph command with a pretty one line option makes the output look much nicer:

    基本图形用法的问题在于,日志的详细信息会妨碍您的操作。 将Git log graph命令与漂亮的一行选项匹配可使输出看起来更好:

    演示地址

    Cameron McKenzie Cameron McKenzie

    Git日志图示例 (Git log graph examples)

    git log --graph --pretty="%ad" --date=shortgit log --graph --pretty="%C(yellow) %s"git log --graph --pretty="%C(bold green) %(ar)"git log --graph --pretty="%C(bold blue)%h" --decorate --all

    演示地址

    Cameron McKenzie Cameron McKenzie

    #6执行浅Git克隆 (#6 Perform a shallow Git clone)

    git clone --depth 5 https://github.com/cameronmcnz/my-github-repo.git

    That’s how you perform a shallow Git clone. You specify a depth as an option in the clone command, and when the shallow Git clone command completes, your local repo will only contain a history of that many commits.

    这就是您执行浅Git克隆的方式。 您可以在clone命令中将深度指定为选项,然后在浅Git clone命令完成时,本地存储库将仅包含该多次提交的历史记录。

    Shallow Git clone benefits include:

    浅Git克隆的好处包括:

    A shallow Git clone makes it easier to search the recent Git history and troubleshoot problems

    浅的Git克隆使搜索最近的Git历史记录和解决问题变得更加容易 Only the Git branch of interest is cloned

    仅克隆目标Git分支A long history of unneeded commits is not downloaded

    不需要提交的悠久历史未下载The disk space used by a shallow Git clone is minimal

    浅Git克隆使用的磁盘空间很小A shallow Git clone runs faster than a normal Git clone

    浅Git克隆的运行速度比普通Git克隆快

    演示地址

    Cameron McKenzie Cameron McKenzie

    #7从其他分支或提交获取单个文件 (#7 Get a Single File From a Different Branch or Commit)

    Messed up a file? No need to go to GitHub or stash changes to look it up in a different branch.

    弄乱了文件? 无需转到GitHub或隐藏更改即可在其他分支中查找它。

    git restore --source=<commit or branch> <file path>

    修复和壁球 (Fixup and Squash)

    This one requires some habits adjustments, but it’s worth it.

    这需要一些习惯调整,但这是值得的。

    While doing code-reviews this is a very common workflow:

    在进行代码审查时,这是一个非常常见的工作流程:

    Push changes in a single commit with a nice message

    在一次提交中推送更改并显示一条好消息 Collect feedback from teammates

    收集队友的反馈Create a bunch of other commits with messages like “fix”, “review”, etc.

    使用“ fix”,“ review”等消息创建一堆其他提交。Squash all commits into the first one with the clean message

    用干净的消息将所有提交压缩到第一个

    Now, on step 3, use git commit --fixup=HEAD. This will re-use the commit messages from the previous commit and add fixup! a prefix to it.

    现在,在第3步中,使用git commit --fixup=HEAD 。 这将重用上一次提交中的提交消息并添加fixup! 它的前缀。

    Next, in step 4, use git rebase -i --autosquash <rebase target>. --autosquash flag will mark fixup! commits to be squashed automatically. You just need to save and quit the rebase editor.

    接下来,在步骤4中,使用git rebase -i --autosquash <rebase target> 。 --autosquash标志将标记--autosquash fixup! 承诺将被自动压缩。 您只需要保存并退出变基编辑器。

    And sure, set up your own shortcut aliases. Real time-saver.

    当然,请设置您自己的快捷方式别名。 实时节省时间。

    #8 git jump获得更好的分支体验 (#8 git jump for Better Experience With Branches)

    git-jump puts recently used branches on top of the list, has fuzzy-search, and interactive UI among other things.

    git-jump将最近使用的分支放在列表的顶部,具有模糊搜索和交互式UI。

    dev.to dev.to

    No setup required, just install and use.

    无需安装,只需安装和使用。

    #9 diff-so-fancy而不是默认Diff (#9 diff-so-fancy Instead of Default Diff)

    Standard diff is fine, but with minimal effort, it can be even better. Take a look, left — standard, right — diff-so-fancy.

    标准diff很好,但是只需最少的努力,它甚至可以变得更好。 看一下,左-标准,右-diff-so-fancy 。

    dev.to dev.to

    Less clutter and more information. Install it and modify the config:

    更少混乱,更多信息。 安装它并修改配置:

    git config --global core.pager "diff-so-fancy | less --tabs=4 -RFX"

    #10 Micro作为默认编辑器 (#10 Micro as Default Editor)

    A modern and intuitive terminal-based text editor for editing files.

    一个现代且直观的基于终端的文本编辑器,用于编辑文件。

    git config --global core.editor micro dev.to dev.to

    #11发生了什么变化? (#11 What changed?)

    The below command will show a log with changes introduced by each commit from the last two weeks.

    下面的命令将显示一个日志,其中包含最近两周内每次提交引入的更改。

    git whatchanged —-since=‘2 weeks ago’

    #12从上一次提交中删除文件 (#12 Remove a file from last commit)

    Let’s say you committed a file by mistake. You can quickly remove that file from the last commit by combining rm and commit --amend commands:

    假设您错误地提交了文件。 您可以通过组合rm和commit --amend命令从上一次提交中快速删除该文件:

    git rm —-cached <file-to-remove>git commit —-amend

    #13寻找分支 (#13 Finding Branches)

    The below command will show all branches that contain a particular commit.

    以下命令将显示包含特定提交的所有分支。

    git branch --contains <commit>

    #14在本地优化存储库 (#14 Optimize the repository locally)

    git gc --prune=now --aggressive

    For more: git help gc

    有关更多信息: git help gc

    #15 Magit (#15 Magit)

    Magit is an interface to the version control system Git, implemented as an Emacs package.

    Magit是版本控制系统Git (作为Emacs软件包实现)的接口。

    #16推荐的Git工作流程概述 (#16 overview of recommended Git workflows)

    An overview of recommended workflows with Git.

    Git推荐工作流程的概述。

    git help workflows

    For further details on GIT go through the below links.

    有关GIT的更多详细信息,请通过以下链接。

    Atlassian Git Tutorial

    Atlassian Git教程

    git-cheat-sheet

    git备忘单

    diff-so-fancy

    差异化

    github-cheat-sheet

    github-备忘单

    git tips

    git技巧

    Little Things I Like to Do with Git

    我喜欢用Git做的小事

    Git Workflows for Pros

    专业人士的Git工作流程

    Git from the inside out

    由内而外的Git

    git-game

    git游戏

    Introduction to Git — talk by Scott Chacon

    Git简介— Scott Chacon的演讲

    Git Tutorial — Git Fu With The Command Line

    Git教程—命令行中的Git Fu

    Git Immersion

    浸入Git

    Learn Enough Git

    学习足够的Git

    git-flight-rules

    git-flight-rules

    gitflow

    gitflow

    进一步阅读 (Further Reading)

    Techtarget

    技术目标

    3-tools-and-2-commands-to-improve-your-git-workflow

    3个工具和2个命令来改善您的git工作流程

    “In theory, theory and practice are the same. In practice, they’re not.”- Yoggi Berra

    “在理论上,理论和实践是相同的。 实际上,它们不是。”-Yoggi Berra

    翻译自: https://codeburst.io/git-tips-and-tricks-to-improve-your-git-workflow-3f8652fa4a62

    git add 和git

    相关资源:jdk-8u281-windows-x64.exe
    Processed: 0.010, SQL: 9