Git Learning Notes

Workflow

When using git to collaborate with others, the following workflow should be taken:

  1. git checkout main to switch the local branch to main.
  2. git pull origin main to pull the remote main branch to the local branch to catch up the work.
  3. git checkout -b newLocalBranchName to create a new local branch that you are going to work on. Usually the name of the branch is related what you are going to do.
  4. Coding. git add . and git commit -m "blablabla"
  5. git push origin newRemoteBranchName to create a new remote branch that you are going to commit your work to. Usually the remote branch name is the same as the local branch name.
    1. In this step, you might commit several time and it’s tedious to type a long git push command. Therefore, you can use git push --set-upstream origin newRemoteBranchName to link the local branch with the remote branch, so that next time you want to commit and push to the remote branch, you can simply use the command git push.
  6. Create a git pull request in github or whatever git platform you use and merge the new remote branch to the remote main branch.
  7. Repeat 1~6 if you are going to work on a different thing in this project.

Init

Create a New Repository

1
2
3
4
5
6
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin <whatever ssh repository address>
git push -u origin main

Push an Existing Repository

1
2
3
git remote add origin <whatever ssh repository address>
git branch -M main # This is how you rename a branch's name. "Main" is preferred because "master" can be offending.
git push -u origin main

.gitignore

The .gitignore file can fail to work from time to time, especially when you want to ignore a file that has been tracked before.

Try the following commands to fix this problem:

1
2
3
4
git rm -r --cached .
git add .
git commit -m "Untracked files issue resolved to fix .gitignore"
git push

What these commands do is that they clear all the cache and reestablish the track during which .gitignore is loaded.

Git Commit Messages in Appropriate Formatting

Reference: How to Write Good Commit Messages: A Practical Git Guide

1
git commit -m '<type>: <verb> <object>'

type:

  • feat: add a new feature

  • fix: fix a bug

  • style: change the coding style without any other functional changes

  • refactor: rewrite

  • test: everything related to testing

  • docs: everything related to documentation

  • chore: regular maintenance

e.g. feat(homepage): add register button

If you are working on something not related to software development, say a lab report, then there’s no such thing as adding a feature. In this case, just use verb+object to describe your work. e.g. add data sheet

Rename a branch

1
git branch -m newNameForBranch

Delete branch

If you want to delete a local branch:

1
git branch -d nameOfYourBranch

If you want to delete a remote branch:

1
git push origin -d nameOfRemoteBranch

Clone from certain branch

1
git clone --branch <branchname> <remote-repo-url>

Create new branch and switch to it

1
git checkout -b anyNameYouLike

List all branches

1
git branch

Switch between different branches locally

1
git checkout nameOfTargetedBranch

Push to certain branch in the upstream (if not exist one, create one)

1
git push origin nameOfTheUpstreamBranch

Set/change upstream

1
git push --set-upstream origin nameOfUpStream

Pull from certain branch

1
git pull origin nameOfTheUpstreamBranch