配置化命令
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倉庫
- 配置forked倉庫
configuring-a-remote-for-a-fork - Merging an upstream repository into your fork
syncing-a-fork
轉載請注明 原文地址