Introduction to git (notes)
Published:
A step by step introduction to git. Also, see reference1 and reference2
Install git
- Download and install git: https://git-scm.com/ or https://npm.taobao.org/mirrors/git-for-windows/
Create ssh key pairs
- Create ssh key pairs: $ ssh-keygen -t ed25519 -C "your_email@example.com"
- Copy information of id_ed25519.pub into SSH keys at your github page
Clone a repository from github to a local directory
- $ git clone https://github.com/xxxxxxx/xxxxxxxx.git
Add files, make changes, pull, push, etc.
- Create new file in your repository: $ touch aFile.md
- Also, if the file aFile.md already exists, you can edit it then excute the following steps
- Restore changes in file (should be used before ‘git add .’): $ git restore aFile.md
- Then the file “aFile.md” is Untracked, we can check it by: $ git status
- Next, we can make it staged by: $ git add .
- After that, we commit the change to the local repository by: $ git commit -m "aFile.md is added"
- Finally, we push the change to the onCloud repository by: $ git push origin master
- If changes have been made onCloud, Pull changes from the onCloud repository by: $ git pull
- This is equal to $ git fetch + git merge
Branch
- See local branches: $ git branch
- See remote/onCloud branches: $ git branch -r
- Create a new branch but stay at the old branch: $ git branch newBranch
- Create a new branch and changed to the new branch: $ git checkout -b newBranch
- Switch a branch: $ git switch newBranch
- Merge a specified branch (sBranch) to the present branch: $ git merge sBranch
- Delete a branch: $ git branch -d branchName
- Delete a remote branch:
- $ git push origin --delete branchName
- $ git branch -dr [remote/branch]
- Branch conflict
- If a file is both updated, commited in different branches, e.g., master and dev, and dev is merged into master, then branch conflict will appear.
- Thus we need to solve the conflict manully: either by artifically modify the target file in master branch, or just click a choice in the opened vscode window.
- Finally, add, commit and push the updated master branch. And delete the dev branch if necessary.
- If the branch dev is onCloud, we can clone/switch locally by: $ git branch dev remotes/origin/dev
- Check the fetch and push urls: $ git remote -v
- After edit and commit, update the local dev to onCloud dev by: $ git push origin dev
Version tag control
- Use git tag use check the current version: $ git tag
- After commit, use git tag use update the new version: $ git tag v1.3.99
- Recheck the version by git tag
- See the detailed change in each version: $ git show v1.3.99
Use alias (optional)
- Change name of git xxxx command by a short one as: $ git config --global alias.[shortName] [commandName]
- For example (git st = git status): $ git config --global alias.st status
Connection errors
- Failed to connect to github.com port 443 after 21098 ms: Timed out
- $ git config –global –unset http.proxy
- $ git config –global –unset https.proxy
- OpenSSL SSL_read: Connection was reset, errno 10054
- $ git config –global http.sslVerify “false”