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 . and git 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 command git push.
Create a git pull request in github 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.
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.
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
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 Related
Push to certain branch in the upstream (if not exist one, create one)