<-
Git Init
下载对应系统版本并安装 git,检查安装情况:
git --version
配置 config 并检查配置情况:
git config --global user.name "Bug Hunter"
git config --global user.email "[email protected]"
git config -l
配置 ssh:
ssh-keygen -t rsa -C "[email protected]"
return * 3
去到 Github - Settings - SSH and GPG keys, new SSH key 将上一步生成的 ~/.ssh 目录下的 id_rsa.pub 文件内容复制到新建的 SSH key 中,并建立连接:
ssh -T [email protected]
接下来就是正常 git 远程仓库操作。
## Creating a New Repository
git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/<user>/<repo>.git
git push --set-upstream origin main
## Viewing History
git log
git blame <file>
## Branching and Merging
git branch -av # show detail information of all branches
git branch ‹new-branch> # create a new branch
git switch <branch-name> # switch to a branch
git checkout -b <new_branch> # create a new branch and switch to it
git branch -d <branch> # Delete a local branch (only if merged)
git branch -D <branch> # Force delete a local branch (even if unmerged)
git merge <branch> # merge branch to current branch
## Undoing Changes
git checkout -- < file> # undo changes before git add
git reset <file> # undo changes between git add and git commit
git revert <commit-hash> # undo specific commit
## Additional Essential Commands
### Cloning a Repository
git clone https://github.com/<user>/<repo>.git
### Pulling Changes from Remote
git pull origin master
### Pushing Changes to Remote
git push origin <branch>
### Stashing Changes
git stash # Stash changes
git stash list # List all stashes
git stash apply # Apply the latest stash
git stash drop # Drop the latest stash
### Tagging
git tag ‹tag-name> # Create a new tag
git tag # List all tags
git push origin <tag-name> # Push tag to remote
### Viewing Differences
git diff # Show changes between working directory and index
git diff HEAD # Show changes between working directory and last commit