1 在遠程服務器上創建空項目"projectname"
登錄https://code.csdn.net/projects/new,根據提示新建空項目。
注:不要勾選【使用 README文件初始化項目】選項
2 本機操作步驟
2.1 從命令行模式進入客戶端項目的根目錄
2.2 Git設置
2.2.1 設置Git提交的默認用戶名
git config --global user.name "xxxxx"
2.2.2 設置Git提交的默認郵箱
git config --global user.email "[email protected]"
2.3 創建並初始化本地倉庫,包含README.md文件
touch README.md //創建README.md文件
git init //創建倉庫並初始化
git add . //添加當前目錄的所有文件到暫存區
git commit -m "first commit" //提交暫存區到本地倉庫
git remote add origin https://code.csdn.net/nokiaxjw/projectname.git //添加遠程git倉庫配置到本機,遠程倉庫名稱"origin"
git push -u origin master //將當前分支push到遠程倉庫的master分支
3 Git命令說明
3.1 遠程倉庫相關命令
git clone [url] [localPath] //從遠程主機克隆一個版本庫,到本地目錄,默認遠程倉庫名稱origin
git clone [url] //從遠程主機克隆一個版本庫,到當前目錄,默認遠程倉庫名稱origin
git clone -o [remoteName] [url] //從遠程主機克隆一個版本庫,到當前目錄,遠程倉庫名稱=remoteName
git remote -v //查看本機的遠程git倉庫配置
git remote show [remoteName]
//查看遠程git倉庫信息
git remote add [remoteName] [url] //添加遠程倉庫remoteName]=origin(遠程倉庫名稱)
git remote rm [remoteName] //刪除遠程倉庫
git remote set-url --push [remoteName] [newUrl] //修改遠程倉庫
git fetch [remoteName] //拉取遠程倉庫的所有更新內容到本地
git fetch [remoteName] [remoteBranchName]
//拉取遠程倉庫的指定分支的更新內容到本地
git pull [remoteName] [remoteBranchName]:[localBranchName] //拉取遠程倉庫的分支的更新內容,併合併到本地分支
git pull [remoteName] [remoteBranchName]
//拉取遠程倉庫的分支的更新內容,併合併到本地當前分支
git push [remoteName] [localBranchName]:[remoteBranchName] //推送本地分支[localBranchName]到遠程倉庫的[remoteBranchName]分支
git push [remoteName] [localBranchName] //推送本地分支[localBranchName]到遠程倉庫的相同名稱分支
git push [remoteName] //推送本地當前分支到遠程倉庫的相同名稱分支
git
push [remoteName] --force //忽略衝突,強行推送本地當前分支到遠程倉庫的相同名稱分支
git
push [remoteName] --all //推送本地所有分支到遠程倉庫
3.2 branch 分支
git branch //列出所有本地分支
git branch -r //列出所有遠程分支
git branch -a //列出所有本地分支和遠程分支
git branch [branchName] //新建一個分支,但不改變當前分支
git checkout -b [branchName] //新建一個分支,並切換到該分支
git checkout [branchName] //切換到指定分支,並更新工作區
git merge [branchName] //合併指定分支到當前分支
git branch -d [branchName] //刪除本地分支
git push [remoteName] --delete [remoteBranchName] //刪除遠程分支
git branch -dr [remoteName/remoteBranchName] //刪除指定的遠程倉庫的遠程分支
git log --graph --pretty=oneline --abbrev-commit //查看分支歷史
3.3 tag 標籤
一般用在master主分支上,用來標識穩定的版本信息
git tag //列出所有tag
git tag [tagName] //在當前commit新建一個tag
git tag [tagName] [commit] //在指定commit位置新建一個tag
git tag -a [tagName] -m "version release" //創建帶說明的標籤
git show [tagName] //查看tagName的信息
git tag -d [tagName] //刪除本地標籤tagName
git push origin --delete tag [remoteTagName] //刪除遠程標籤retmoteTagName
git push [remoteName] [tagName] //推送指定tagName到遠程服務器
git push [remoteName] --tags //推送所有標籤到遠程服務器
git checkout -b [branch] [tagName] //新建一個分支,指向某個tagName
3.4 git工作現場保存和恢復
一般用在臨時有個緊急BUG要修正,然而當前工作區修改沒有完成(不想提交未修改好的內容到遠程服務器),
把當前未修改好的工作區內容入棧,修正BUG並提交到遠程服務器
git stash //將當前的工作區內容保存到Git棧中,然後從最近的一次提交中讀取相關內容,讓工作區和上次提交的內容一致
git stash apply //從Git棧中讀取最近一次保存的內容,恢復工作區的相關內容
git stash pop //從Git棧中讀取最近一次保存的內容,恢復工作區的相關內容,然後刪除Git棧最近一次保存的內容
git stash list //顯示Git棧內的所有備份
git show stash@{0} //顯示Git棧最近一次保存的內容
git stash drop //刪除Git棧最近一次保存的內容
git stash clear //清空Git棧