Git

2 minute read

1 Commit Rules

1.1 Commit Message Format

<type><(scope)>: <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

1.2 Type

Type indicates the class of current git commit.

Must be one of the following:

  • build: Changes involved with the build system
  • ci: Changes to the CI configuration files and scripts
  • docs: Changes involved with documents
  • feat: A new feature
  • fix: A bug fix
  • perf: Changes that improves performance of the code
  • refactor: Rewriting the code that neither adds a new feature nor fixes a bug
  • style: Changes that not affect the function of the code and only affect the format of the code
  • test: Adding new test code or correcting existed test code

1.3 Scope(optional)

Scope should be the scope of the code affected by this git commit. The scope is different in different project.

1.4 Subject

Subject contains a succinct description of the change:

  • Use the imperative and present sense: change not changes nor changed.
  • Don’t capitalize the first letter.
  • Don’t dot “.” at the end.

1.5 Body(optional)

Just as in the subject, use the imperative and present sense. The body should include the motivation of the change.

1.6 Footer(optional)

Footer contains BREAKING CHANGES and is the place to refer issues. The format of issue referring is Close #<issue-num> and in case of multiple issues is Close #<issue-num>, #<issue-num>.

FAQ

1. Push a existed repository to a remote repository.

  1. Create a repository on Github.
  2. Add remote repository by git remote add <name> <url>.
  3. Fetch remote repository by git fetch <remote-repository>.
  4. Set the upstream of current local branch by git branch -u <remote-name>/<remote-branch>.
  5. Git pull to merge upstream by git pull --allow-unrelated-histories.
  6. Git push current local branch by git push.

2. Rename a local branch.

# Git checkout branch which will be renamed.
git branch -m <new-name>

3. Change the default branch name created by git init.

git config --global init.defaultBranch <name>

4. Amend the code for which you don’t want to add a new commit.

git add --all
git commit --amend
git push -f

5. Change the default name created by the command git init with Git of version later than 2.28.

# For the current repository
git init --initial-branch=<name>
# Or
git init -b <name>
# Global setting for all repositories
git config --global init.defaultBranch <name>

6. Git commit with editor.

Set export EDITOR='vim' to ~/.zshrc to set the default editor. And use git commit instead of git commit -m.

7. Git fetch.

Fetch remote repositories.

  1. Fetch all of the branches from the remote repository:

    git fetch <remote-repository>
    
  2. Fetch the specific branch from the remote repository:

    git fetch <remote-repository> <remote-branch>
    

Updated: