git 基础使用

5 minute read

Published:

git 简易教程。

  • origin:源仓库,一般为被 clone 的仓库

  • upstream:上游仓库,一般为被 fork 的仓库

  • master:本地分支
  • origin/master:远程分支

gh 命令使用

# 安装 gh
brew install gh

# 创建仓库
gh repo create public-repo

# 创建私有仓库
gh repo create private-repo --private

# 删除仓库
gh repo delete repo

配置信息

Git的设置文件为 .gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。

# 显示当前的 Git 配置
git config --list
 
# 编辑 Git 配置文件
git config -e [--global]
 
# 查看提交代码时的用户信息
git config --global user.name
git config --global user.email

# 设置提交代码时的用户信息
git config --global user.name "[name]"
git config --global user.email "[email address]"

创建版本库

# 在当前目录新建一个Git代码库
git init
 
# 新建一个目录,将其初始化为Git代码库
git init [project-name]

# 下载一个项目和它的整个代码历史
git clone [url]

添加 / 删除

# 添加指定文件到暂存区
git add [file1] [file2] ...
 
# 添加指定目录到暂存区,包括子目录
git add [dir]
 
# 添加当前目录的所有文件到暂存区
git add .
 
# 删除工作区文件,并且将这次删除放入暂存区
git rm [file1] [file2] ...
 
# 改名文件,并且将这个改名放入暂存区
git mv [file-original] [file-renamed]

代码提交

# 提交暂存区到仓库区
git commit -m [message]
 
# 提交暂存区的指定文件到仓库区
git commit [file1] [file2] ... -m [message]

分支

# 列出所有本地分支
git branch
 
# 列出所有远程分支
git branch -r
 
# 列出所有本地分支和远程分支
git branch -a
 
# 新建一个分支,但依然停留在当前分支
git branch [branch-name]
 
# 新建一个分支,并切换到该分支
git checkout -b [branch]
 
# 新建一个分支,指向指定commit
git branch [branch] [commit]
 
# 新建一个分支,与指定的远程分支建立追踪关系
git branch --track [branch] [remote-branch]
 
# 切换到指定分支,并更新工作区
git checkout [branch-name]
 
# 切换到上一个分支
git checkout -
 
# 建立追踪关系,在现有分支与指定的远程分支之间
git branch --set-upstream [branch] [remote-branch]
 
# 合并指定分支到当前分支
git merge [branch]
 
# 选择一个commit,合并进当前分支
git cherry-pick [commit]
 
# 删除分支
git branch -d [branch-name]
 
# 删除远程分支
git push origin --delete [branch-name]
git branch -dr [remote/branch]

标签

# 列出所有 tag
git tag

# 查看 tag 信息
git show [tag]

# 新建一个 tag 在当前 commit
git tag [tag]
 
# 删除本地 tag
git tag -d [tag]

# 删除远程 tag
git tag -d [tag]  # 第一步:删除本地库标签
git push origin :refs/tags/[tag]  # 第二步:从远程库删除标签
 
# 推送 tag 到远程
git push [remote] [tag]

# 推送所有 tag 到远程
git push [remote] --tags

查看信息

# 显示有变更的文件
git status
 
# 显示当前分支的版本历史(详情: commit id + Author + Date + comment)
git log

# 显示当前分支的版本历史(简写:commit id + comment)
git log --pretty=oneline

# 查看分支合并图
git log --graph

# 查看分支合并缩略图
git log --graph --pretty=oneline --abbrev-commit

# 显示暂存区和工作区的差异
git diff

# 显示暂存区和上一个 commit 的差异
git diff --cached [file]
 
# 显示工作区与当前分支最新commit之间的差异
git diff HEAD
 
# 显示两次提交之间的差异
git diff [first-branch]...[second-branch]
 
# 显示今天你写了多少行代码
git diff --shortstat "@{0 day ago}"
 
# 显示某次提交的元数据和内容变化
git show [commit]
 
# 显示某次提交发生变化的文件
git show --name-only [commit]
 
# 显示某次提交时,某个文件的内容
git show [commit]:[filename]
 
# 显示当前分支的最近几次提交
git reflog

远程同步

 # 下载远程仓库的所有变动
git fetch [remote]
 
# 显示所有远程仓库
git remote -v
 
# 显示某个远程仓库的信息
git remote show [remote]
 
# 增加一个新的远程仓库,并命名
git remote add [shortname] [url]
 
# 取回远程仓库的变化,并与本地分支合并
git pull [remote] [branch]
 
# 上传本地指定分支到远程仓库
git push [remote] [branch]
 
# 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --force
 
# 推送所有分支到远程仓库
git push [remote] --all

撤销

 # 恢复暂存区的指定文件到工作区
git checkout [file]
 
# 恢复某个commit的指定文件到暂存区和工作区
git checkout [commit] [file]
 
# 恢复暂存区的所有文件到工作区
git checkout .
 
# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
git reset [file]
 
# 重置暂存区与工作区,与上一次commit保持一致
git reset --hard
 
# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
git reset --hard [commit]
 
# 新建一个commit,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
git revert [commit]
 
# 暂时将未提交的变化移除,稍后再移入
git stash
git stash pop

GitHub SSH Key 设置

第1步:创建 SSH Key

查看 ~/.ssh 目录下有没有 id_rsaid_rsa.pub 这两个文件,如果已经有了可直接跳到下一步。如果没有请创建 SSH Key:

cd ~/.ssh
ssh-keygen -t rsa -C "[email address]"

创建 SSH Key 成功后 ~/.ssh 目录下有 id_rsaid_rsa.pub 这两个文件,这两个就是 SSH Key 的秘钥对,id_rsa 是私钥,不能泄露出去,id_rsa.pub 是公钥,可以放心地告诉任何人。

第2步:添加 SSH Key

登陆 GitHub -> 点击右上角的”个人头像” -> Settings -> SSH and GPG Keys -> New SSH key

填上任意 Title,在 Key 文本框里粘贴 id_rsa.pub 文件的内容即可。

GitHub Personal Access Token 设置

错误:

remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.

第1步:在 Github 中创建 Personal Access Token

GitHub account => Settings => Developer Settings => Personal Access Token => Generate New Token => Fillup the form => click Generate token => Copy the generated Token

第2步:在 macOS 上配置

Open the Spotlight => Type Keychain access then press the Enter key to launch the app => In Keychain access, search for github.com => Find the internet password entry for github.com => Edit the entry accordingly

Github 公有仓库私有化

第1步:在 Github 中创建一个私有仓库 private-repo

gh repo create private-repo --private

第2步:将公有仓库 public-repo 的内容推送到私有仓库 private-repo 即可

git clone https://github.com/exampleuser/public-repo.git
cd public-repo
git config --local remote.origin.url https://github.com/yourname/private-repo.git
git push origin master

Github Fork 公有仓库私有化

第1步:在 Github 中创建一个私有仓库 private-repo

gh repo create private-repo --private

第2步:将公有仓库 public-repo 的内容推送到私有仓库 private-repo 即可

git clone --bare https://github.com/exampleuser/public-repo.git
cd public-repo.git
git push --mirror https://github.com/yourname/private-repo.git
cd ..
rm -rf public-repo.git

第3步:接下来便能 clone 私有仓库做自己想做的修改

git clone https://github.com/yourname/private-repo.git
cd private-repo
make some changes
git commit
git push origin master

补充:从公有仓库中 pull 新的更新

cd private-repo
git remote add public https://github.com/exampleuser/public-repo.git
git pull public master
git push origin master

删除 gitignore 中已存在于仓库的内容

第1步:查看有哪些内容

git ls-files -i -c --exclude-from=.gitignore

第2步:进行删除

git ls-files -i -c --exclude-from=.gitignore | xargs git rm --cached
git commit -m 'Removed all files that are in the .gitignore' 
git push origin master

参考资料及致谢

Git教程

初学Git——命令总结

Git常用命令及方法大全

GitHub: How to make a fork of public repository private?

How to remove files that are listed in the .gitignore but still on the repository?

Support for password authentication was removed. Please use a personal access token instead

Creating a personal access token

Updating credentials from the macOS Keychain