Git Interference Free Tricks and Rules

Last updated on:2 months ago

I concluded some Git commands techniques and interference free rules from doing projects.


About Git

Frok and Init

mkdir GrdProcess # 创建GrdProcess目录
cd GrdProcess # 切换到GrdProcess目录
git init # 创建并初始化git库
git remote add origin # 添加远程git仓库
ssh-keygen -t rsa -C "your register email" # 添加SSH秘钥到git远程库,邮箱可以从git账号里查看
cat ~/.ssh/ # 查看秘钥

复制添加到你git账号里的ssh key列表里,就可以通过安全认证传输数据了。参考Git如何fork别人的仓库并作为贡献者提交代码

git pull origin main # 将远程git库代码下载到本地 (origin代表远程仓库,main代表主分支)

Ignore Irrelevant Files




git add newfilename.type
git commit -m 'commit'
git push -u origin main


git remote add upstream # 添加上游仓库地址
git remote -v # 查看 origin 和 upstream 对应的仓库是否正确
git pull upstream main # 从上游仓库获取最新的代码合并到自己本地仓库的main分支上
git push -u origin main


git push --force

Delete and Create Files

git rm file1.txt
git commit -m "remove file1.txt"


New Branch

In Git 1.7.0 and later, you can checkout a new branch:

git checkout -b <branch>

Edit files, add and commit. Then push with the -u (short for --set-upstream) option:

git push -u origin <branch>

Git will set up the tracking information during the push.

Reference: How do I push a new local branch to a remote Git repository and track it too?

New Remote Git Repo

git remote set-url origin

Interference Free Rules

Camel Case

For variables, use upper camel case, eg. MyVariables, CamelCase

For functions’ names, use lower camel case, eg. myFunction(), camelCase()

GitHub Collaboration

Step 1: Before you push your local repo

Firstly, please check the others’ pull requests.

They haven’t changed your new files or the changed parts are different

In this case, merge their pull request into the default repo, and then pull the default repo to merge with your local one. You should know that it won’t change the files you have changed. Instead, it will show you

branch            main       -> FETCH_HEAD
Already up to date.

The parts you’ve changed were also changed by them

This is intriguing and should be taken care. At that time, you are supposed to back up the files you have changed, and merge their repo to the default repo. Then,

git pull origin main


git pull upstream main

After that, you can change whatever you want reference to your backup ones.

Step 2: Pull Request

Create a new pull request, so that the others can saw. Sometimes, you can’t be sure that your codes must be right. Thus, Other contributors can give you some comments to help you enhance your work. After that, if everything is fine, then they will help you merge the pull request.

Some questions

  1. What if there is no commit by others, when the next time you push? — Jump to step 2.

简而言之,就是创建pull request不要自己merge自己的branch到default branch。


Anthon Dave