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 "xxxxx@xxx.com"
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栈