起源:Git上想創建分支,不會。因為公司的簡單粗暴,於是記錄學習過程
三步驟,本地創建分支-- git branch main,
本地代碼切換分支 -- git checkout main,
push到git上 -- git push origin main
檢查是否push成功:git branch -r
附上一些git基礎命令行
#git新建代碼 git init # 在當前目錄新建一個Git代碼庫 git init [project-name] # 在當前目錄新建一個目錄,將其初始化為Git代碼庫 git clone [url] # 下載一個項目和它的整個代碼歷史
#git配置
git config --list # 顯示當前的Git配置
git config -e [--global] # 顯示Git配置文件
git config [--global] user.name "[name]" # 設置提交代碼時的用戶名
git config [--global] user.email "[email address]" # 設置提交代碼時的用戶郵箱
#git操作文件的增刪改
git add [file1] [file2] ... # 添加指定文件到暫存區
git add [dir] # 添加指定目錄到暫存區,包括子目錄
git add . # 添加當前目錄的所有文件到暫存區
git add -p # 添加每個變化前,都會要求確認,對於同一個文件的多處變化,可以實現分次提交
git rm [file1] [file2] ... # 刪除工作區文件,並且將這次刪除放入暫存區
git rm --cached [file] # 停止追蹤指定文件,但該文件會保留在工作區
git mv [file-originname] [file-newname] # 改名文件,並且將這個改名放入暫存區
#git 代碼提交操作
git commit -m [message] # 提交暫存區到本地倉庫
git commit [file1] [file2] ... -m [message] # 提交暫存區指定文件到本地倉庫
git commit -a # 提交工作區自上次commit之后的變化,直接到本地倉庫
git commit -v # 提交時顯示所有diff信息
git commit --amend -m [message] # 使用一次新的commit,替代上一次提交,如果代碼沒有任何變化,則用來改寫上一次commit的提交信息
git commit --amend [file1] [file2] ... # 重做上一次commit,並包括指定文件的新變化
#git遠端操作
git fetch [remote] # 下載遠程倉庫的所有變動,注意這個時候是不會修改本地文件的
git pull [remote] [branch] # 拉取遠程倉庫的變化,並與本地分支合並
git remote -v # 顯示所有遠程倉庫
git remote show [remote] # 顯示某個遠程倉庫的信息
git remote add [shortname] [url] # 增加一個新的遠程倉庫,並命名
git push [remote] [branch] # 上傳本地指定分支到遠程倉庫
git push [remote] --force # 強行推送當前分支到遠程倉庫,即使有沖突
git push [remote] --all # 推送所有分支到遠程倉庫
git push <remote> :<branch/tag-name> # 刪除遠程分支或標簽
git push --tags # 上傳所有標簽
#git,push后的回退操作 謹慎使用
git reset --hrad HEAD # 撤銷工作目錄中所有未提交文件的修改內容
git checkout HEAD <file> # 撤銷指定的未提交文件的修改內容
git revert <commit> # 撤銷指定的提交
git log --before="1 days" # 退回到之前1天的版本
git checkout [file] # 恢復暫存區的指定文件到工作區
git checkout [commit] [file] # 恢復某個commit的指定文件到暫存區和工作區
git checkout . # 恢復暫存區的所有文件到工作區
git reset [file] # 重置暫存區的指定文件,與上一次commit保持一致,但工作區不變
git reset --hard # 重置暫存區與工作區,與上一次commit保持一致
git reset [commit] # 重置當前分支的指針未指定commit,同時重置暫存區,但工作區不變
git reset --hard [commit] # 重置當前分支的HEAD未指定commit,同時重置暫存區和工作區,與指定commit一致
git reset --keep [commit] # 重置當前HEAD未指定commit,但保持暫存區和工作區不變
#git中查看操作信息
git status # 查看當前工作區狀態(與暫存區對比,增加刪除或修改)
git log # 顯示當前分支的版本歷史
git log --stat # 顯示commit歷史,以及每次commit發生變更的文件
git log -S [keyword] # 根據關鍵字搜索提交歷史
git log [tag] HEAD --pretty=format:%s # 顯示某個commit之后的變動,每個commit占據一行。我記得--pretty=online也行
git log -p [file] # 顯示指定文件相關的每一個diff
git log -5 --pretty --oneline # 顯示過去5次提交
git shortlog -sn # 顯示所有提交過的用戶,按提交次數排序
git blame [file] # 顯示指定文件是什么人在什么時間修改過,這個blame很生動形象
git diff # 顯示暫存區和工作區的差異
git diff --cached [file] # 顯示暫存區和上一個commit的差異
git diff HEAD # 顯示工作區與當前分支最新commit之間的差異
git diff [first-branch]...[second-branch] # 顯示兩次提交之間的差異
git diff --shortstat "@{0 day agp}" # 顯示今天你寫了多少航代碼
git show [commit] # 顯示某次提交的元數據和內容變化
git show --name-only [commit] # 顯示某次提交發生變化的文件
git show [commit]:[filename] # 顯示某次提交時,某個文件的內容
git reflog # 顯示當前分支的最近幾次提交
#git中tag操作
git tag # 列出所有本地標簽
git tag <tagname> # 基於最新提交創建標簽
git tag -d <tagname> # 刪除標簽
git push origin :refs/tags/[tagName] # 刪除遠程tag
git show [tag] # 查看tag信息
git push [remote] [tag] # 提交指定tag
git push [remote] --tags # 提交所有tag
git checkout -b [branch] [tag] # 新建一個分支,指向某個tag
#git分支操作
git branch # 顯示所有本地分支
git branch -r # 列出所有遠程分支
git branch -a # 列出所有本地分支和遠程分支
git branch [branch-name] # 新建一個分支,但head依然停留在當前分支
git branch --track [branch] [remote-branch] # 新建一個分支,與指定的遠程分支建立追蹤關系
git branch -d [branch-name] # 刪除指定分支
git checkout -b [branch-name] # 新建一個分支,並切換head到該分支
git checkout [branch-name] # 切換head到指定分支,並更新工作區
git checkout - # 切換到上一個分支
git branch --set-upstream [branch] [remote-branch] # 建立追蹤關系,在現有分支與指定的遠程分支之間
git merge [branch] # 合並指定分支到當前分支
git rebase <branch> # 衍合指定分支到當前分支
git cherry-pick [commit] # 選擇一個commit,合並進當前分支