Workflow
When using git to collaborate with others, the following workflow should be taken:
git checkout main
to switch the local branch to main.git pull origin main
to pull the remote main branch to the local branch to catch up the work.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.- Coding.
git add .
andgit commit -m "blablabla"
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.- 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 commandgit push
. - Create a
git pull
request ingithub
or whatever git platform you use and merge the new remote branch to the remote main branch. - Repeat 1~6 if you are going to work on a different thing in this project.
Init
Create a New Repository
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
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:
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
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
Branch Related
Rename a branch
git branch -m newNameForBranch
Delete branch
If you want to delete a local branch:
git branch -d nameOfYourBranch
If you want to delete a remote branch:
git push origin -d nameOfRemoteBranch
Clone from certain branch
git clone --branch <branchname> <remote-repo-url>
Create new branch and switch to it
git checkout -b anyNameYouLike
List all branches
git branch
Switch between different branches locally
git checkout nameOfTargetedBranch
Push Related
Push to certain branch in the upstream (if not exist one, create one)
git push origin nameOfTheUpstreamBranch
Set/change upstream
git push --set-upstream origin nameOfUpStream
Pull Related
Pull from certain branch
git pull origin nameOfTheUpstreamBranch