Git - eodeluga/dev-notes GitHub Wiki

Git commit message prefixes

Prefix Purpose Typical Usage
feat A new feature or capability Adding a new function, endpoint, component, etc.
fix A bug fix Resolving a defect, regression, or incorrect behaviour.
docs Documentation only changes Updating README, code comments, API docs, etc.
style Code style or formatting Changing whitespace, indentation, naming, etc. (no logic changes).
refactor Code change that neither fixes a bug nor adds a feature Improving structure, readability, or design without altering behaviour.
perf A change that improves performance Optimisations such as caching, reducing complexity, etc.
test Adding or correcting tests Unit, integration, or snapshot tests.
build Changes that affect the build system or external dependencies Adjusting npm scripts, build pipelines, dependencies, or tooling.
ci Continuous Integration / deployment related changes GitHub Actions, Jenkins, CircleCI, etc.
chore Routine tasks or maintenance Version bumps, dependency updates, configuration tweaks, etc.
revert Reverting a previous commit Undoing a change with the commit hash.

Changing a commit message

Commit has not been pushed online

If the commit only exists in your local repository and has not been pushed to your online repository, then you can amend the commit message with the git commit --amend command.

  1. On the command line, navigate to the repository that contains the commit you want to amend.

  2. Enter the following:

    git commit --amend

  3. In your text editor, edit the commit message, and save the commit.

You can add a co-author by adding a trailer to the commit. For more information, see "Creating a commit with multiple authors."

You can create commits on behalf of your organization by adding a trailer to the commit. For more information, see "Creating a commit on behalf of an organization"

The new commit and message will appear in your repository host the next time you push.

Amending older or multiple commit messages already pushed

If you have already pushed the commit, you will have to force push a commit with an amended message.

We strongly discourage force pushing, since this changes the history of your repository. If you force push, people who have already cloned your repository will have to manually fix their local history.

Changing the message of the most recently pushed commit

  1. Follow the steps above to amend the commit message.

  2. Use the push --force-with-lease command to force push over the old commit.

    $ git push --force-with-lease origin example-branch

Changing the message of older or multiple commit messages

If you need to amend the message for multiple commits or an older commit, you can use interactive rebase, then force push to change the commit history.

  1. On the command line, navigate to the repository that contains the commit you want to amend.

  2. Use the git rebase -i HEAD~n command to display a list of the last n commits in your default text editor.

# Displays a list of the last 3 commits on the current branch
$ git rebase -i HEAD~3

The list will look similar to the following:

pick e499d89 Delete CNAME
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.

# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
  1. Replace pick with reword before each commit message you want to change.
pick e499d89 Delete CNAME
reword 0c39034 Better README
reword f7fde4a Change the commit message but push the same commit.
  1. Save and close the commit list file.

In each resulting commit file, type the new commit message, save the file, and close it.

When you're ready to push your changes to GitHub, use the push --force command to force push over the old commit.

$ git push --force origin example-branch

Creating a branch

Creating local branch

git branch localBranchName

Create remote upstream branch

git push --set-upstream origin localBranchName

Update remote tracking branch

git push -u origin localBranchName

Deleting a branch

Delete merged branch

git branch -d branchName

Delete merged branches except a specified one

git branch | grep -v "develop" | xargs git branch -d

Delete branch even if not fully merged

git branch -D branchName

Delete remote branch

git push origin --delete remoteBranchName

Delete a particular remote-tracking branch

git branch -d -r origin/mybranch

Delete stale remotes

git remote prune origin

Merging branches

  • Ensure that you are in the branch that you want to merge the changes into.

git branch

Note: Use the following to switch to the correct branch if you are not

git checkout {branch_name}
  • Merge the changes `git merge {branch_name_to_merge_into_current_branch}

Git will then try to automatically merge the changes from the specified branch into the current branch. If there are any conflicts between the changes, Git will notify you and prompt you to resolve them manually.

  • Once you have resolved any conflicts, add and commit the changes using
git add
git commit
  • Push the changes to the remote repository git push

Note: It is always recommended to create a backup branch or clone of the repository before performing a merge in case anything goes wrong.