我可以编辑两个文件,然后使用基于 GitHub 的 Web 编辑器提交一个文件吗?

GitHub 具有基于 Web 的文件编辑的优秀特性。但是,在编辑完一个文件之后,我似乎必须提交这个文件的更改,然后才能继续。有没有一种方法,我可以编辑两个或更多的文件,然后使一个提交的所有更改他们?

我认为这会更好,因为太多不必要的提交是令人讨厌的。

26946 次浏览

The GitHub Web Flow supports one file per commit. To add multiple files to a single commit, you will need to clone the repository locally, edit the files, then commit and push.

The command line would be like this:

  1. cd to the directory

  2. Stage all modified files with

    git add <file-1> <file-2> <etc>
    
  3. Commit with

    git commit -m'<your-message>
    

    or if you want to launch an external editor

    git commit
    
  4. Send the history/commit with

    git push
    

There is a web-based workaround:

  1. Create a temporary branch, switch to it;
  2. Edit multiple files, commit each file separately;
  3. Make pull request;
  4. Under Merge pull request button, choose Squash and merge (if the repo allows it), then confirm merge;
  5. Delete temporary branch.

I use this method on a PC which I don't want to install git on.

Addendum to what @VictorIstomin and @MarCnu were saying:

If you want to edit a number of filenames instead of the contents of those files, simply follow the instructions that Victor laid out, but you won't have to "Confirm Squash and Merge". You can just hit "Confirm Merge" (which should be the only option, since you didn't actually edit file contents.

The question has been answered well, just wanted to clarify this for any future viewers who might be changing filenames, which is something I commonly do in GitHub.

Yes there is a way to edit 2 files, when you've already edited your first file in the web browser view of GitHub. It's based on the instructions here:

https://webapps.stackexchange.com/questions/95940/how-can-i-change-multiple-files-and-submit-them-in-one-pull-request-on-github

The summary and clarified steps are:

  1. Edit your first file, by clicking the Edit icon in another user's repository. Save this edit in your first commit. GitHub will automatically create a new branch/fork for you, like "patch-1".
  2. Do not create a Pull Request yet.
  3. [This part is the most difficult, because it's not obvious] Go to the list of repositories in your profile, at github.com ➔ click icon at top-right ➔ Your Repositories. Then go to the forked repository that you've just made, and switch to the "patch-1" branch.
  4. Continue making changes to another file, and commit it to your "patch-1" branch. When you are done, click the "Pull Requests" tab near the top, then click Compare & Pull Request or Create Pull Request. You should see your 2 commits in this Pull Request.
  5. If you don't see a "Create Pull Request" button, open a URL like this in your web browser: https://github.com/TargetPerson/TargetGitRepo/compare/master...MyGithubUsername:patch-1
  6. This will show a view to compare your changes to the original target branch in the other user's repository. Then press the Create Pull Request button.

Let's assume the repo in question is https://github.com/repoUsername/repoName

  1. Fork the repository by clicking the Fork button at the top of the repo page.

enter image description here

  1. Edit as many files as you want via Web-based file editor in your fork of the repo. Commit each file individually.

  2. Go to https://github.com/repoUsername/repoName/pulls and press the big green New pull request button.

enter image description here

  1. On the next page click compare across forks link.

enter image description here

  1. Then in Choose a Head Repository field select your fork

enter image description here

  1. Create pull request button will appear. Click it.

enter image description here

  1. Enter title and description of your pull request and click Create pull request button again.

enter image description here

If you don't wish to create a temporary branch and merge the changes (which results in a bunch of pull requests in the repository that were just used to merge commits), you can upload multiple (modified) files and GitHub will create a single commit with them.

Note that, when uploading files, the files will be uploaded to the current folder visible on GitHub.

The disadvantage of this method is that, when you need to modify files that are in different directories, you will need to either upload all your project files (which will be slow) or create a skeleton directory of your project and place the modified files there (so that GitHub modifies the correct files and does not create a new file).

Be careful when using this method because GitHub will not show you your changes until after you have committed the files.

The new and better approach is to use the GitHub integrated online VSCode editor.

  1. Press . (period) on your keyboard when viewing your repository. This launches an in-browser VSCode instance (on github.dev).
  2. Make your changes across multiple files, just as you would do locally.
  3. Use the sidebar on the left to select the Git menu, and commit/push through the GUI as normal. You can also create a Pull Request there if you don't have write permissions.

enter image description here