Category Archives: git

single PR single commit

When you are using git in a forking workflow, your local commits and pushes will end up on your fork of the repository you are contributing to.

In general a PR should contain single commit (at least the project that I have worked on) so before you send your PR you will generally want all your commits (changes) in one single commit. In this case there are two options, one is to “squash” all your commits into a single commit or the alternative is to continuously amend each commit to your first one.

This can be accomplished by first adding the changes you want to amend:

git add < filename>

After which you can simply use the below command to amend these changes to you previous commit without updating the original commit message.

git commit --amend --no-edit

NOTE: Once you amend to a previous commit your subsequent pushes will fail. This is because your local branch is out of sync with the remote branch. To fix this you will be need to add the force flag, be aware that this will overwrite the remote branch with the local changes.

git push --force

Further detailed info can be found here:

git – keeping a fork in sync

Commands to keep a fork in sync with the upstream repo

To keep it up to date with the original source repo (the one you forked it from):

git remote add <your local name for the upstream> <URL for the upstream repo>

To check you set the remote repo as the upstream for your fork:

git remote -v

To check which branch you are working on locally:

git status

Pull changes from the upstream branch:

git pull <your local name for the upstream> <local branch name>

If you accidentally specify the incorrect URL for the remote upstream you forked you can change it by doing the following (as seen when using git remote -v):

git remote set-url <your local name for the upstream> <correct URL for the upstream repo>

At this point you have pulled all changes from the remote upstream repo locally to your machine.  You then need to push these changes to your remote fork on github:

git push