Git Interference Free Tricks and Rules
Last updated on:5 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 https://github.com/Davidietop/GrdProcess.git # 添加远程git仓库
ssh-keygen -t rsa -C "your register email" # 添加SSH秘钥到git远程库,邮箱可以从git账号里查看
cat ~/.ssh/id_rsa.pub # 查看秘钥
复制添加到你git账号里的ssh key列表里,就可以通过安全认证传输数据了。参考Git如何fork别人的仓库并作为贡献者提交代码。
git pull origin main # 将远程git库代码下载到本地 (origin代表远程仓库,main代表主分支)
Ignore Irrelevant Files
请使用.gitignore文件,将你在当前文件夹不想上传的文件或子文件夹给屏蔽掉,vscode中这些文件夹会变成灰色。
Push
一般步骤:
git add newfilename.type
git commit -m 'commit'
git push -u origin main
每次修改前,注意使用先把上游仓库合并到本地仓库。推荐每次代码待提交前,都从原项目拉取一下最新的代码。
git remote add upstream https://github.com/beenoera/GrdProcess.git # 添加上游仓库地址
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"
增加文件直接加就好了,然后也是commit
Git add all except
git add -u
git reset -- .\checkArchi.py
New Branch
In Git 1.7.0 and later, you can checkout a new branch:
git checkout -b <branch>
Delete branch:
git branch -d <branchname>
Switch branch:
git switch <branchname>
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 git@your.git.repo.example.com:user/repository2.git
Git submodule
git submodule add https://github.com/chaconinc/DbConnector to/your/folder
Use git status to see few things
git status
Finally, it would generalize a .gitmodules file on the root folder.
[submodule "GitHub/Inception-v4"]
path = GitHub/Inception-v4
url = https://github.com/titu1994/Inception-v4
Git remove deleted fileds
Use:
git add -u
And then commit and push.
Reference: How to remove multiple deleted files in Git repository
Filename capitalization changes ignored in git Git
Backup the file, and then delete the file on the repo.
git add --all
git add -u
git commit -m "delete file"
Copy backup file back in the same directory
git add --all
git commit -m "rename xx to Xx"
git push -u origin main
Cancel commit
git reset HEAD~1
Change the capitalisation of filename
git mv --force myfile MyFile
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
or
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
- What if there is no commit by others, when the next time you push? — Jump to step 2.
简而言之,就是创建pull request,不要自己merge自己的branch到default branch。
Cheers,
Anthon Dave
Reference
[1] 7.11 Git Tools - Submodules
[2] How do you change the capitalization of filenames in Git?
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!