git命令
版本控制
Git 簡介
Git是目前世界上最先進的分布式版本控制系統。
如果你是小白,請先看這里: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
專用名詞
- Workspace: 工作區
- Index / Stage: 暫存區
- Repository: 倉庫區 ( 本地版本庫 )
- Remote: 遠程倉庫 ( 遠程倉庫 / 遠程版本庫 )
Git 工作流
Fork a repo
https://help.github.com/en/articles/fork-a-repo
Creating a pull request from a fork
https://help.github.com/en/articles/creating-a-pull-request-from-a-fork
代碼庫
在當前目錄新建一個Git代碼庫
git init
新建一個目錄,將其初始化為Git代碼庫
git init [project-name]
克隆代碼庫, 默認克隆所有分支
git clone [url]
克隆指定分支代碼
git clone [url] -b [branch name]
eg.
git clone https://github.com/vuejs/vuex.git -b dev
克隆默認分支最新一次提交
git clone [url] --depth=1
eg.
git clone https://github.com/reiinakano/fast-style-transfer-deeplearnjs.git --depth=1
克隆指定分支指定層數
git clone -b [branch name] [url] --depth=1
克隆 dev 分支最近一次提交 eg.
git clone -b dev https://github.com/vuejs/vuex.git --depth=1
Git 配置
Git的設置文件為 .gitconfig
,它可以在用戶主目錄下(全局配置),也可以在項目目錄下(項目配置)。
顯示當前Git配置
git config --list
編輯Git配置文件
git config -e [--global]
設置代碼時的用戶信息 建議設置此項,后面的操作會很方便
git config [--global] user.name "[name]"
git config [--global] user.email "[email address]"
增加/刪除文件
操作之前先執行 git status
查看一下狀態
添加文件到暫存區 建議使用這種方式進行操作
git add [file1] [file2] ...
添加所有改動到暫存區
git add .
刪除工作區文件,并且將這次刪除放入暫存區
git rm [file1] [file2] ...
停止追蹤指定文件,但該文件會保留在工作區
git rm --cached [file]
代碼提交
操作之前先執行 git status
查看一下狀態
提交暫存區到倉庫區
git commit -m [message]
提交暫存區的指定文件到倉庫區
git commit [file1] [file2] ... -m [message]
重做上一次commit,并包括指定文件的新變化
git commit --amend [file1] [file2] ...
分支
列出所有本地分支
git branch
列出所有遠程分支
git branch -r
列出所有本地分支和遠程分支
git branch -a
新建一個分支,但依然停留在當前分支
git branch [branch-name]
新建一個分支,并切換到該分支
git checkout -b [branch]
切換到指定分支,并更新工作區
git checkout [branch-name]
合并指定分支到當前分支
git merge [branch]
刪除本地分支
git branch -d [branch-name]
刪除遠程分支
git push origin --delete [branch-name]
或者
git branch -dr [remote/branch]
恢復刪除的分支 Git 分支刪除后恢復
git branch new_branch commitId
查看信息
查看改動的文件
git status
查看當前分支的版本日志
git log
查看單行日志
git log --pretty=oneline
查看日志主題
git log --pretty=format:%s
查看 commit 的最新編號
git rev-parse HEAD
顯示暫存區和工作區的差異
git diff
顯示今天寫了多少行代碼
git diff --shortstat "@{0 day ago}"
git log --author="$(git config --get user.name)" --no-merges --since=1am --stat
過濾 commit . git log --grep
git log --grep chore
git log --grep --author="John"
標簽
列出現有標簽
git tag
創建一個含附注類型的標簽
git tag -a v0.5.0 -m 'version 0.5.0'
git show
顯示各種類型的對象。查看相應標簽的版本信息,同時顯示打標簽時的提交對象
git show v0.5.0
push
單個 tag
git push origin [tagname]
eg:
git push origin v1.0 #將本地v1.0的tag推送到遠端服務器
push
所有 tag
, 如果不起作用,在 Git
控制臺確認賬號是否有權限推送 Tag
git push origin --tags
刪除本地標簽
git tag -d [tagName]
刪除遠程標簽
git push origin :refs/tags/[tagName]
遠程同步
下載遠程倉庫的所有變動
git fetch [remote]
顯示所有遠程倉庫
git remote -v
修剪 - 刪除與 origin 相關的陳舊引用
git remote prune origin
拉取遠程倉庫的變化,并與本地分支合并
git pull [remote] [branch]
上傳本地指定分支到遠程倉庫
git push [remote] [branch]
強行推送當前分支到遠程倉庫,即使有沖突 謹慎使用
git push [remote] --force
推送所有分支到遠程倉庫
git push [remote] --all
Git 證書問題
臨時生效,退出shell后失效
export GIT_SSL_NO_VERIFY=true
永久生效
echo 'export GIT_SSL_NO_VERIFY=true' >> ~/.bashrc
win10 出現證書問題 解決方式
git config --global http.sslVerify false
撤銷
恢復暫存區的指定文件到工作區
git checkout [file]
恢復某個commit的指定文件到暫存區和工作區
git checkout [commit] [file]
恢復暫存區的所有文件到工作區
git checkout .
重置暫存區的指定文件,與上一次commit保持一致,但工作區不變
git reset [file]
重置暫存區與工作區,與上一次commit保持一致
git reset --hard
回滾到指定版本
git reset --hard [commitId]
重建版本庫
rm -rf .git
git init
git add .
git commit -m 'git init first commit'
git remote add origin <github_repo_url>
git push -f -u origin master
添加遠程倉庫
cd existing_folder
git init
git remote add origin git@git.sg-ai.com:ywguo/Test.git
git add .
git commit -m "Initial commit"
git push -u origin master
Stashing
儲藏
儲藏當前工作區
git stash
查看儲藏狀態
git stash list
恢復儲藏工作區狀態
git stash apply
如何創建公鑰
首先啟動一個Git Bash窗口(非Windows用戶直接打開終端)
執行:
cd ~/.ssh
如果返回“… No such file or directory”,說明沒有生成過SSH Key,直接進入第4步
否則進入第3步備份備份:
mkdir key_backup
mv id_isa* key_backup
- 生成新的Key:(引號內的內容替換為你自己的郵箱)
ssh-keygen -t rsa -C "your_email@youremail.com"
輸出顯示:
>Generating public/private rsa key pair. Enter file in which to save the key
(/Users/your_user_directory/.ssh/id_rsa):<press enter>
直接回車,不要修改默認路勁。
>Enter passphrase (empty for no passphrase):<enter a passphrase>
Enter same passphrase again:<enter passphrase again>
設置一個密碼短語,在每次遠程操作之前會要求輸入密碼短語!閑麻煩可以直接回車,不設置。
- 成功:
Your identification has been saved in /Users/your_user_directory/.ssh/id_rsa.
Your public key has been saved in /Users/your_user_directory/.ssh/id_rsa.pub.
The key fingerprint is:
... ...
-
提交公鑰:
6.1 找到.ssh文件夾,用文本編輯器打開“id_rsa.pub”文件,復制內容到剪貼板。
6.2 打開 https://github.com/settings/ssh ,點擊 Add SSH Key 按鈕,粘貼進去保存即可。
git設置用戶名密碼
設置 git 用戶名/郵箱
git config --global user.name [username]
git config --global user.email [email]
但是這個僅僅是設置用戶名密碼,如果你的Git 源每次操作需要你輸入用戶名/密碼驗證,你依然需要每次設置,那么該如何辦呢?
git 保存用戶名密碼
這里主要是配置一個 config 項
有兩個方法,基本上原理都是一樣,都是修改 .git/config 文件
1.使用如下命令,修改 config 文件即可保存
echo "[credential]" >> .git/config
echo " helper = store" >> .git/config
2.直接修改 .git/config 文件
在 Linux/mac 下可以直接使用 vim 工具修改 config 文件
ubuntu@VM-7-212-ubuntu:~/kernel-code/kernel-netfilter-sample-code$ vim .git/config
##修改成如下
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://github.com/Miss-you/kernel-netfilter-sample-code.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
##主要是后面這兩行,如果不想保存,則刪除即可
[credential]
helper = store
##保存
這樣就可以保存用戶名密碼,不用每次都輸入了!
git config
查看配置
使用 git config --list
查看已設配置
feiqianyousadeMacBook-Pro:xt_GTPU yousa$ git config --list
core.excludesfile=/Users/yousa/.gitignore_global
user.name=Miss-you
user.email=snowfly1993@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=https://github.com/Miss-you/xt_GTPU.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
Git 修改默認編輯器為 vim
git config --global core.editor vim
操作規范
提交到倉庫 description
需要有意義命名
git commit -m [description]
分支請及時合并清理
git merge [branch]
git push [remote] [branch]
git branch -d [branch-name]
git push origin --delete [branch-name]
友情提示: 目前只是現在工作中用到的命令整理,如果沒有您需要的,請自行 google
-
文檔持續更新中,歡迎大家拍磚 ...
?