add-and-commit

add-and-commit

GitHub Action自动化Git提交工具

add-and-commit是一个GitHub Action工具,用于在工作流中自动执行Git操作。它支持自定义提交信息、作者信息,灵活管理文件的添加和删除。此外,该工具还提供分支创建、标签管理和推送控制等功能,简化了CI/CD流程中的代码更新操作。add-and-commit为开发者提供了一种高效的方式来自动化Git相关任务。

GitHub Actions代码提交自动化工作流版本控制CI/CDGithub开源项目

Add & Commit

All Contributors

You can use this GitHub Action to commit changes made in your workflow run directly to your repo: for example, you use it to lint your code, update documentation, commit updated builds, etc...

Table of contents

Inputs

Add a step like this to your workflow:

- uses: EndBug/add-and-commit@v9 # You can change this to use a specific version. with: # The arguments for the `git add` command (see the paragraph below for more info) # Default: '.' add: 'src' # The name of the user that will be displayed as the author of the commit. # Default: depends on the default_author input author_name: Author Name # The email of the user that will be displayed as the author of the commit. # Default: depends on the default_author input author_email: mail@example.com # Additional arguments for the git commit command. The --message argument is already set by the message input. # Default: '' commit: --signoff # The name of the custom committer you want to use, if different from the author of the commit. # Default: the name of the author (set with either author_name or default_author) committer_name: Committer Name # The email of the custom committer you want to use, if different from the author of the commit. # Default: the email of the author (set with either author_email or default_author) committer_email: mail@example.com # The local path to the directory where your repository is located. You should use actions/checkout first to set it up. # Default: '.' cwd: './path/to/the/repo' # Determines the way the action fills missing author name and email. Three options are available: # - github_actor -> UserName <UserName@users.noreply.github.com> # - user_info -> Your Display Name <your-actual@email.com> # - github_actions -> github-actions <email associated with the github logo> # Default: github_actor default_author: github_actor # Arguments for the git fetch command. If set to false, the action won't fetch the repo. # For more info as to why fetching is usually recommended, please see the "Performance on large repos" FAQ. # Default: --tags --force fetch: false # The message for the commit. # Default: 'Commit from GitHub Actions (name of the workflow)' message: 'Your commit message' # If this input is set, the action will push the commit to a new branch with this name. # Default: '' new_branch: custom-new-branch # The way the action should handle pathspec errors from the add and remove commands. Three options are available: # - ignore -> errors will be logged but the step won't fail # - exitImmediately -> the action will stop right away, and the step will fail # - exitAtEnd -> the action will go on, every pathspec error will be logged at the end, the step will fail. # Default: ignore pathspec_error_handling: ignore # Arguments for the git pull command. By default, the action does not pull. # Default: '' pull: '--rebase --autostash ...' # Whether to push the commit and, if any, its tags to the repo. It can also be used to set the git push arguments (see the paragraph below for more info) # Default: true push: false # The arguments for the `git rm` command (see the paragraph below for more info) # Default: '' remove: './dir/old_file.js' # Arguments for the git tag command (the tag name always needs to be the first word not preceded by an hyphen) # Default: '' tag: 'v1.0.0 --force' # Arguments for the git push --tags command (any additional argument will be added after --tags) # Default: '' tag_push: '--force'

Git arguments

Multiple options let you provide the git arguments that you want the action to use. It's important to note that these arguments are not actually used with a CLI command, but they are parsed by a package called string-argv, and then used with simple-git.
What does this mean for you? It means that strings that contain a lot of nested quotes may be parsed incorrectly, and that specific ways of declaring arguments may not be supported by these libraries. If you're having issues with your argument strings you can check whether they're being parsed correctly either by enabling debug logging for your workflow runs or by testing it directly with string-argv (RunKit demo): if each argument and option is parsed correctly you'll see an array where every string is an option or value.

Adding files

The action adds files using a regular git add command, so you can put every kind of argument in the add option. For example, if you want to force-add a file: ./path/to/file.txt --force.
The script will not stop if one of the git commands doesn't match any file. E.g.: if your command shows a "fatal: pathspec 'yourFile' did not match any files" error the action will go on.
You can also use JSON or YAML arrays (e.g. '["first", "second"]', "['first', 'second']") to make the action run multiple git add commands: the action will log how your input has been parsed. Please mind that your input still needs to be a string because of how GitHub Actions works with inputs: just write your array inside the string, the action will parse it later.

Deleting files

The remove option can be used if a predetermined list of files needs to be removed. It runs the git rm command, so you can pass every kind of argument with it. As if with the add input, you can also use JSON or YAML arrays to make the action run multiple git rm commands.

If you want deleted files to be auto-detected and committed, you can use the --no-ignore-removal/-A git arguments.

Pushing

By default the action runs the following command: git push origin ${new_branch input} --set-upstream. You can use the push input to modify this behavior, here's what you can set it to:

  • true: this is the default value, it will behave as usual.
  • false: this prevents the action from pushing at all, no git push command is run.
  • any other string:
    The action will use your string as the arguments for the git push command. Please note that nothing is used other than your arguments, and the command will result in git push ${push input} (no remote, no branch, no --set-upstream, you have to include them yourself).

One way to use this is if you want to force push to a branch of your repo: you'll need to set the push input to, for example, origin yourBranch --force.

Creating a new branch

If you want the action to commit in a new branch, you can use the new_branch input.

Please note that if the branch exists, the action will still try push to it, but it's possible that the push will be rejected by the remote as non-straightforward.

If that's the case, you need to make sure that the branch you want to commit to is already checked out before you run the action.
If you're really sure that you want to commit to that branch, you can also force-push by setting the push input to something like origin yourBranchName --set-upstream --force.

If you want to commit files "across different branches", here are two ways to do it:

  1. You can check them out in two different directories, generate your files, move them to your destination and then run add-and-commit in the destination directory using the cwd input.
  2. You can manually commit those files with git commands as you would on your machine. There are several ways to do this depending on the scenario. One of them if to stash your changes, checkout the destination branch, and popping the stash. You can then use the add-and-commit action as usual. Please note that this is just an example and may not work for you, since your use case may be different.

Tagging

You can use the tag option to enter the arguments for a git tag command. In order for the action to isolate the tag name from the rest of the arguments, it should be the first word not preceded by an hyphen (e.g. -a tag-name -m "some other stuff" is ok).
You can also change the arguments of the push command for tags: every argument in the tag_push input will be appended to the git push --tags command.
For more info on how git arguments are parsed, see the "Git arguments" section.

Outputs

The action provides these outputs:

  • committed: whether the action has created a commit ('true' or 'false')
  • commit_long_sha: the full SHA of the commit that has just been created
  • commit_sha: the short 7-character SHA of the commit that has just been created
  • pushed: whether the action has pushed to the remote ('true' or 'false')
  • tagged: whether the action has created a tag ('true' or 'false')
  • tag_pushed: whether the action has pushed a tag ('true' or 'false')

For more info on how to use outputs, see "Context and expression syntax".

FAQs

Working with PRs

By default, when you use actions/checkout on a PR, it will checkout the head commit in a detached head state. If you want to make some changes, you have to checkout the branch the PR is coming from in the head repo.
You can set it up like this:

- uses: actions/checkout@v4 with: repository: ${{ github.event.pull_request.head.repo.full_name }} ref: ${{ github.event.pull_request.head.ref }}

You can find the full docs for payloads of pull_request events here.

If you're planning on running this only on "internal" PRs (where head and base are in the same repo) then you can omit the repository input.
If you're planning to use this with PRs coming from other forks, please keep in mind that you might not have write access to those repos. You can try setting up the repo with your PAT, as explained in the "About tokens" paragraph of this section.

Keep in mind that this "custom checkout" is meant only for PRs: if your workflow runs on multiple events (like push or workflow_dispatch), you could try having this step run only for pull_request events, while other ones will trigger the usual checkout.
If you wish to do so, you can use the step.if property, here's the docs.

About tokens

When pushing, the action uses the token that the local git repository has been configured with: that means that if you want to change it you'll need to do it in the steps that run before this action. For example: if you set up your repo with actions/checkout then you have to add the token there.
Changing the token with which the repo is configured can be useful if you want to run CI checks on the commit pushed by this action; anyway, it has to be set up outside of this action.

The action automatically gets the GitHub token from a github_token input: this input should not be modified by the user, since it doesn't affect the commits as it's only used to access the GitHub API to get user info, in case they selected that option for the commit author.

The commit from the action is not triggering CI!

That's because you're checking out the repo using the built-in GITHUB_TOKEN secret: GitHub sees that the push event has been triggered by a commit generated by CI, and doesn't run any further checks to avoid unintentional check loops.

If you're sure that you want the commits generated during CI to trigger other workflow runs, you can checkout the repo using a Personal Access Token (PAT): this will make the resulting commit the same as if you made it yourself.
If you're using actions/checkout, check out their docs to see how to set your repo token.

About actions/checkout

The token you use when setting up the repo with this action will determine what token add-and-commit will use.
Some users reported that they were getting an error:

> fatal: could not read Username for 'https://github.com': No such device or address

If you're getting this error and you're using actions/checkout@v1, try upgrading to actions/checkout@v2. If you're still having problems after upgrading, feel free to open an issue. Issue ref: #146

Performance on large repos

By default, the action will fetch the repository before starting to work on it: this ensures that it can see the already existing refs.

When working with a repository that has a lot of branches and tags, fetching it can take a long time. If the fetch step is taking too much time, you can decide to skip it by setting the fetch input to false: this will prevent the action from running git fetch altogether.

Please note that you have to set up your workflow accordingly: not fetching the repo can impact branch and tag creation within the action, and for this reason it's recommended to disable it only if necessary. Issue ref: #386

Examples

Different author/committer configurations

If you don't want to use your GitHub username for the CI commits, you can use the default_author input to make it appear as if it was made by "GitHub Actions", by setting its value to github_actions.

<img src="https://user-images.githubusercontent.com/26386270/115738624-80b51780-a38d-11eb-9bbe-77461654274c.png" height=40/>
on: push jobs: build: runs-on: ubuntu-latest steps: - uses: EndBug/add-and-commit@v9 with: default_author: github_actions

You can also use the committer_name and committer_email inputs to make it appear as if GitHub Actions is the committer, here are a couple of example steps:

<img src="https://user-images.githubusercontent.com/26386270/130594168-1d910710-e2d0-4b06-9324-cbe5dde59154.png" height=70/>
- uses: EndBug/add-and-commit@v9 with: message: Show GitHub Actions logo committer_name: GitHub Actions committer_email: actions@github.com
<img src="https://user-images.githubusercontent.com/26386270/130594443-b881fae7-3064-4020-a4cc-6db37ef0df65.png" height=70/>
- uses: EndBug/add-and-commit@v9 with: message: Show GitHub logo committer_name: GitHub Actions committer_email: 41898282+github-actions[bot]@users.noreply.github.com

Array inputs

Due to limitations in the GitHub action APIs, all inputs must be either strings or booleans. The action supports arrays in add and remove, but they have to be encoded as a string with a YAML flow sequence:

- uses: EndBug/add-and-commit@v9 with: add: '["afile.txt", "anotherfile.txt"]'

(note the single-quotes) or a YAML block sequence:

- uses: EndBug/add-and-commit@v9 with: add: | - afile.txt - anotherfile.txt

(Note the pipe character making it a multiline string.)

Automated linting

Do you want to lint your JavaScript files, located in the src folder, with ESLint, so that fixable changes are done without your intervention? You can use a workflow like this:

name: Lint source code on: push jobs: run: name: Lint with ESLint runs-on: ubuntu-latest steps: - name: Checkout repo uses: actions/checkout@v4 - name: Set up Node.js uses: actions/setup-node@v4 - name: Install dependencies run: npm install - name: Update source code run: eslint "src/**" --fix - name: Commit changes uses: EndBug/add-and-commit@v9 with:

编辑推荐精选

TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

热门AI工具生产力协作转型TraeAI IDE
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI助手AI工具AI写作工具AI辅助写作蛙蛙写作学术助手办公助手营销助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

聊天机器人AI助手热门AI工具AI对话
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

热门AI工具AI办公办公工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

模型训练热门AI工具内容创作智能问答AI开发讯飞星火大模型多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

AI助手热门AI工具AI创作AI辅助写作讯飞绘文内容运营个性化文章多平台分发
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多