git add

Git add (Git添加)

Description

Description

The git add is a command, which adds changes in the working directory to the staging area. With the help of this command, you tell Git that you want to add updates to a certain file in the next commit. But in order to record changes, you need to run git commit too. In combination with the commands mentioned above, git status command is also needed to see which state the working directory and the staging area are in.

Working principles

Working principles (工作原理)

The git add, along with git commit are used to record project versions into the history of the repository. They are must-know for every Git user, as they compose the basis of Git workflow.

While developing a project, you first edit the files in the working directory, then when a copy of the current state is ready to save, you need to stage changes, which is done with the git add command. The git add command should be called every time altering a file.

Staging area

Staging area (集结区)

The basic role of the git add command is promoting changes in the working directory to the git staging area. The staging area of Git is one of its most special characteristics, which lets gather all the connected changes into highly focused snapshots. Only after that, you can commit these changes to your project history.

Common options

Common options (通用选项)

git add <file>

Examples of git add

Examples of git add

The git add and git commit commands are used to create an initial commit of the current directory, as well as recording changes to existing files.

After starting the project you can add new files by passing the path to git add.

git add hello.py
git commit

Interactive mode

Interactive mode (交互模式)

An interactive staging session lets you choose portions of a file to be added to the next commit. You will be suggested a chunk of changes and prompted for a command. The available options are the following ones:

  • y - staging the hunk (- y -演出大块头)

  • n - not staging the hunk (- n -不上演大块头)

  • q - quit; not staging the hunk or any remaining one

  • a - staging the hunk with all later hunks in the file (- a -将该区块与所有后来的区块一起存放在文件中)

  • d - not staging the hunk or any later hunk in the file (- d -不在文件中暂存区块或任何后来的区块)

  • g - selecting a hunk to go to (-g -选择要转到的区块)

  • / - searching for a hunk which matches the given regex (-/-搜索与给定正则表达式匹配的区块)

  • j - leaving the hunk undecided, checking the next undecided hunk (- j -让大块头犹豫不决,检查下一个犹豫不决的大块头)

  • J - leaving the hunk undecided, checking the next hunk (- J -让大块头犹豫不决,检查下一个大块头)

  • k - leaving the hunk undecided, checking the previous undecided hunk (- k -让大块头犹豫不决,检查上一个犹豫不决的大块头)

  • K - leaving the hunk undecided, checking the previous hunk (- K -让大块头犹豫不决,检查上一个大块头)

  • s - splitting the current hunk into smaller ones (- s -将当前区块拆分为较小的区块)

  • e - manually editing the current hunk (- e -手动编辑当前区块)

  • ? - the print help (-? -打印帮助)

Editing patches

Editing patches (编辑补丁)

Calling git add -e or selecting e from the interactive chunk selector opens a patch in the editor; after exiting the editor, the output is applied to the index. Arbitrary changes are made to the patch, but some changes may have complicated outputs or even an output in a patch that is inapplicable. If you want to decline the operation entirely, simply delete all lines of the patch. Here are some common things you may see in a patch, and which editing operations make sense on them.

Lines starting with “+” represent added content. You can delete them in order to prevent staging any addition lines. (以“+”开头的行表示添加的内容。您可以删除它们,以防止暂存任何附加行。)

Lines beginning with “-” represent removed content. In order to prevent staging their deletion, you can convert the “-” to a " " (space). (以“-”开头的行表示已删除的内容。为了防止暂存其删除,您可以将“-”转换为“” (空格)。)

Modified content is shown with “-” lines (deleting the old content) followed by “+” lines (adding the replacement content). For preventing the staging of the modification, you can convert “-” lines to " “, and remove “+” lines. Note that modifying only half of the pair may cause confusing changes to the index. (修改后的内容显示为“-”行(删除旧内容) ,后跟“+”行(添加替换内容)。为了防止修改的暂存,您可以将“-”行转换为“” ,并删除“+”行。请注意,仅修改配对的一半可能会导致对索引的混淆性更改。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部