随着github的使用频率日益上升,不少刚入门的git开发者会出现各种各样的问题,比如‘没有更新代码就直接上传后产生冲突’、‘分支混淆’等。本文针对这一现象对git的各类操作命令进行一个详细的引导。
GIT全局设置
<!-- 设置开发者的名称 -->
git config --global user.name 'luoyang'
<!-- 设置开发者的邮箱地址 -->
git config --global user.email 'luoyang@sx985.com'
创建新版本库
<!-- 从github上克隆项目 -->
git clone http://120.55.40.55/luoyang/my-test-program.git
<!-- 添加暂存 -->
git add .
<!-- 添加commit -->
git commit -m '测试commit'
<!-- 将代码提交至master分支 -->
git push -u origin master
已存在的文件夹
<!-- 初始化git仓库 -->
git init
<!-- 重建origin远程仓库 -->
git remote add origin http://120.55.40.55/luoyang/my-test-program.git
git add .
git commit -m '测试commit'
git push -u origin master
已存在的GIT版本库
<!-- 将之前的origin仓库名改为old-origin -->
git remote rename origin old-origin
git remote add origin http://120.55.40.55/luoyang/my-test-program.git
git push -u origin --all
git push -u origin --tags
如果你已经修改了代码,但是你的分支还没有切换过来,那你可以进行以下操作
<!-- 查看当前分支情况 -->
git branch
<!-- 将改动存入暂存区 -->
git stash
<!-- 切换到development分支 -->
git checkout development
<!-- 从development分支上更新代码 -->
git pull (origin development)
<!-- 创建一个新的origin-name分支 -->
git checkout -b origin-name
<!-- 从暂存区获取出来 -->
git stash pop
git add .
git commit -m '新的commit'
<!-- 将修改上传到github中的origin-name分支上 -->
git push origin origin-name
其他单独命令部分如下:
<!-- 查看最近2条commit -->
git log -2
<!-- 恢复到某个commit -->
git reset <commit-ID号>
<!-- 删除文件 -->
git rm <文件名>
<!-- 删除某一分支 -->
git branch -d <分支名>
以上是我为大家准备的关于git仓库使用的一些基本操作,熟练掌握这些命令就可以在工作中正常的利用git进行代码管理了。