git add

添加文件到暂存区

  1. # 添加某个文件到暂存区,后面可以跟多个文件,以空格区分
  2. git add xxx
  3. # 添加当前更改的所有文件到暂存区。
  4. git add . 复制代码

git commit

# 提交暂存的更改,会新开编辑器进行编辑
git commit
# 提交暂存的更改,并记录下备注 
git commit -m "you message" 
# 等同于 git add . && git commit -m 
git commit -am 
# 对最近一次的提交的信息进行修改,此操作会修改commit的hash值 
git commit --amend

git pull

# 从远程仓库拉取代码并合并到本地,可简写为 git pull 等同于 git fetch && git merge 
git pull <远程主机名> <远程分支名>:<本地分支名>
# 使用rebase的模式进行合并
git pull --rebase <远程主机名> <远程分支名>:<本地分支名>

git fetch

# 获取远程仓库特定分支的更新
git fetch <远程主机名> <分支名>
# 获取远程仓库所有分支的更新
git fetch --all

git branch

# 新建本地分支,但不切换
git branch <branch-name> 
# 查看本地分支
git branch
# 查看远程分支
git branch -r
# 查看本地和远程分支
git branch -a
# 删除本地分支
git branch -D <branch-nane>
# 重新命名分支
git branch -m <old-branch-name> <new-branch-name>

git rebase 交互模式

讲一个分支上产生了很多的无效的提交,使用 rebase 的交互式模式可以把已经发生的多次提交压缩成一次提交(但是最好只能在自己使用的 feature 分支上进行 rebase 操作,不允许在集成分支上进行 rebase,因为这种操作会修改集成分支的历史记录。)
进入交互式模式的方式是执行:

git rebase -i <base-commit>

git revert/reset

git revert 撤销某次操作,此操作不会修改原本的提交记录,而是会新增一条提交记录来抵消某次操作。

# 针对普通commit
git revert <commit-id>
# 针对merge的commit
git revert <commit-id> -m

git revert会新建一条 commit 信息,来撤回之前的修改。
git reset会直接将提交记录退回到指定的 commit 上。
对于个人的 feature 分支而言,可以使用git reset来回退历史记录,之后使用git push --force进行推送到远程,但是如果是在多人协作的集成分支上,不推荐直接使用git reset命令,而是使用更加安全的git revert命令进行撤回提交。这样,提交的历史记录不会被抹去,可以安全的进行撤回。

git alias

基本用法是

git config --global alias.<简化的字符> 原始命令

如下面的例子:

$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch

可通过配置替换到 .gitconfig文件里的[alias]所属的区域,然后就可以愉快的使用了~

[alias]
st = status -sb
co = checkout
br = branch
mg = merge
ci = commit
ds = diff --staged
dt = difftool
mt = mergetool
last = log -1 HEAD
latest = for-each-ref --sort=-committerdate --format=\"%(committername)@%(refname:short) [%(committerdate:short)] %(contents)\"
ls = log --pretty=format:\"%C(yellow)%h %C(blue)%ad %C(red)%d %C(reset)%s %C(green)[%cn]\" --decorate --date=short
hist = log --pretty=format:\"%C(yellow)%h %C(red)%d %C(reset)%s %C(green)[%an] %C(blue)%ad\" --topo-order --graph --date=short
type = cat-file -t
dump = cat-file -p
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

参考来源 https://juejin.cn/post/6974184935804534815#heading-18