Github & git

Created: 16 Jan 2025

Author:  Chen Xie

创建仓库

SSH

#SSH
#Generate your key with your email.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
#all key file under ~/.ssh/
#id_rsa is private key, don't give it to anyone.  
#id_rsa.pub is public key for GitHub

# different location to generate key Files
ssh-keygen -t rsa -f ~/Documents/sshkey/{name}

将文件夹下的id_rsa.pub文件内容加入到github中。

image

Init & Config

#进入项目文件夹
git init

#Config
# name,email 涉及contribution统计
git config --global user.name "Chen Xie"
git config --global user.email "chenxie2016@163.com"

# 查看设置
git config --list

# 设置
# check the Remote URL AND modify the Remote URL
git remote -v
git remote set-url origin <NEW_GIT_URL_HERE>

# simple for current branch, matching for all branches
git config --global push.default simple

# sensitive to up/lower case
git config core.ignorecase false

Add, Commit & Push

# git add all files 
git add .

# commit
git commit -m "first commit"

git push origin main

.gitignore

File Content

# Mac_OS
.DS_Store
.Spotlight-V100
.Trashes
._*

# Windows
Thumbs.db
desktop.ini
*.[oa] *~  # Gedit
.*.sw[po]  # Vim
# Eclipse
.project
.classpath
.settings
.metadata

# VS Code
.vscode/
*.code-workspace

#IDEA
.idea/
# nodejs
node_modules/
dist/
build/
.env
.env.local
.env.*.local
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# java compile
*.class
target

# Mobile Tools for Java (J2ME)
.mtj
.tmp/
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# jekyll https://jekyllrb.com/tutorials/using-jekyll-with-bundler/#commit-to-source-control
_site/
.sass-cache/
.jekyll-cache/
.jekyll-metadata
.bundle/
vendor/

Remove files which are tracked before it is added to .gitignore

git rm -r --cached .
git add .

Branch

本地基本操作

# list all branches
git branch -a

# change the default branch
git config --global init.defaultBranch {branchName}

# basic branching and merging
git checkout -b iss53
# Switched to a new branch "iss53"
# do modifying and add commit things

# Mergin
git checkout main
# Switched to branch 'master'
git merge iss53
# merge iss53 to main

# Delete
git branch -d [branch-name] #can delete if the branch is merged
git branch -D [branch-name] #can delete without merging the branch

获取远程仓库的分支

# 要clone远程仓库时,如果想要特定分支时,先clone整个仓库,在切换到对应分支.以MaxKB为例
git clone https://github.com/1Panel-dev/MaxKB

# 查看远程有哪些分支
git branch -a

# 新建本地分支,链接远程分支
git checkout -b release-2.0 origin/release-2.0

本地分支与远程仓库分支交互

# Check the relationship between local-branch and remote-branch
git branch -vv

# Add remote Repo
git remote add <repo-nickname> <repoUrl>
# Delete remote Repo
git remote remove <repo-nickname>

# set the relationship between local-branch and remote-branch
git branch --set-upstream-to=<RepoName>/<remote-branch-name> <local-branch>
#or Use -u when push
git push -u <remote> <local_branch>

# Delete local-fetched remote-branches
git branch -d -r origin/[remote-branch-name]

# Delete remote branches in Repository
git push origin --delete [remote-branch-name]

# Delete Fetched Remote Branches
git fetch --prune # Auto-delete branches which are deleted in remote repository

当分支提交了Commit,主干也有新的Commit(git rebase -i

参考:

image

结论,在此情况下,使用git rebase​好过git merge main

黄金原则:不要使用rebase处理已经被其他人引用的提交。

引申,git rebase -i常用命令:pick、rework、squash、drop

image

Recovery

For Entire Folder

git reset --hard # discard all changes
git clean -f -d #remove untracked
git clean -f -d -x # above plus ignored file

For File

# last commit
git checkout filename

# one commit
git log
# choose one commit to recover
git checkout --HEAD filename

For Commit point

#TODO
git reset

# git github