史上最全git命令集

配置化命令

ssh-keygen
git config --global user.name "whalefall541"
git config --global user.email "jackchen541@sina.com"
git config --global alias.ll "log --graph --pretty=format:'%C(yellow)%h%Creset -%C(cyan)%d%Creset %s %Cgreen(%an, %cr)' --abbrev-commit"
git config --global alias.a '!git add -A && git commit -m'

配置全局.gitignore

# 这里最好写绝对路径 有时候可能不生效
git config --global core.excludesfile D:/project/.gitignore 

# .gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。
# 解决方法就是先把本地缓存删除(改变成未track状态),然后再提交:

git rm -r --cached .
git add .

查看类命令

# 查看 
git log --graph --pretty=format:'%C(yellow)%h%Creset -%C(cyan)%d%Creset %s %Cgreen(%an, %cr)' --abbrev-commit
# 查看 xxx提交的内容
git show <commit-id>
# 查看引用记录
git reflog
# 查看文件差异
git diff filename

# 查看已经add 但是未commit 的差异
git diff --staged
git diff --cached
# 仅查看汇总统计
git diff --stat branch1 branch2

# 查看两个提交之间某个文件的差异 第一个为开始的hash,要比当前查看的还早一个
git diff <commit> <commit> xxx.java
# 查看两个分支的具体差异
git diff dev st 


# 一次diff出全部modify的文件内容 也可以自行重定向到某个文件中
git status | awk -F "modified:" '{if($2 != "") print $2}' | xargs git diff

撤销类命令

# 撤销掉modify状态
git checkout <filename>
# 撤销掉add的文件(如果是新文件则是untracked状态 否则是modify状态)
git restore --staged test.txt
git rm --cached test.txt

# 如果commit了 发现某个文件不对那么可以对单个文件再修改再次commit,
# 然后rebase <commit-id>之后的commit(不包括<commit-id>)

git rebase -i <commit-id>
# 撤销 add commit modify(把commit 记录全删掉 慎用)
git reset --hard <commit-id>
# 撤销add、commit  (把commit 记录变成modify之后)
git reset --mixed <commit-id>
# 仅撤销commit (把commit 记录变成add之后)
git reset --soft <commit-id>

# 撤销一次提交内容
git revert <commit-id>

# 删除本地一些记录
git clean -df

创建或删除相关命令

# 本地仓库关联到远程仓库
git remote add origin git@github.com:whalefall541/rebase-learn.git
# 删除关联
git remote rm origin

# 删除远程 分支
git push origin -d <branch-name>

# 创建本地 分支
git branch <branch-name>
# 创建并切换
git chekcout -b <branch-name>
# 删除本地 分支
git branch -D  <branch-name>

合并类命令

# 关闭自动合并
git merge --no-ff -m "merge with no-ff" dev

# 自动变基 在当前分支重放另一个分支 
git rebase [<branch-name> | <commit-id> | <head~n>]

# 交互式变基
git rebase -i <commit-id>
`一般使用 p s组成 将多条commit 合并为一条`
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor

# 拉取代码时变基 fetch and rebase 
git pull -r origin main

# 将base分支的内容变基到当前分支 rebase 就是移动HEAD指针
git rebase <base-branch> <current-branch>

# rebase --onto 可以将一个位于子分支的分支变基到主分支上
# 变基前:current-upsteam-branch 是base-branch的一个子分支 
# 而 current-branch 又是 current-upsteam-branch的一个子分支
# 变基后 current-upsteam-branch current-branch 各自为base-branch 的子分支
git rebase --onto <base-branch> <current-upsteam-branch> <current-branch>

# 从其他分支复制某个提交到另一个分支
git cherry-pick <commit-id>

暂存内容命令

git stash
git stash list
git pop

如何暂存部分文件呢 stash part

提交相关命令

git add .
git commit -m "your description"
git push -u origin main

# 直接在dev分支 推到所有其他分支 从branch1分支推送到branch2
git push origin refs/heads/branch1:branch2

标签类命令

# 在当前分支当前提交上打标签:
git tag v1.0

#如果想要打标签在某个指定历史commit上:
git tag v0.9 f52c633

# 可以通过如下命令查看一个tag信息:
git show v0.1

# 如果标签打错了,也可以删除:
git tag -d v0.1

# 如果要推送某个标签到远程,使用命令git push origin <tagname>
git push origin v1.0
# 或者,一次性推送全部尚未推送到远程的本地标签:
git push origin --tags	
# 如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除:
git tag -d v0.9	
# 然后,从远程删除。删除命令也是push,但是格式如下:
git push origin :refs/tags/v0.9

如何同步github fork仓库

  1. 配置forked仓库
    configuring-a-remote-for-a-fork
  2. Merging an upstream repository into your fork
    syncing-a-fork

转载请注明 原文地址

posted @ 2021-09-10 10:11  WhaleFall541  阅读(314)  评论(0编辑  收藏  举报