常用Git命令(by 星空武哥)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lsyz0021/article/details/72530673

转载请标注原创地址:http://blog.csdn.net/lsyz0021/article/details/72530673

使用Git很久了一直没有总结使用的命令,所以有些长时间不用就会忘记,虽然经常操作用客户端,但是有时候命令还是不错的,写这篇博客不为别的只为记录平时用的的Git命令,方便以后使用查找。

配置git名称和邮箱

git config --global user.name "lsyz0021"
git config --global user.email "[email protected]"

clone命令

git clone https://github.com/lsyz0021/EventBusUtils.git eventbus
git clone ssh://example.com/path/to/repo.git/
git clone git://example.com/path/to/repo.git/
git clone /opt/git/project.git 
git clone file:///opt/git/project.git
git clone ftp[s]://example.com/path/to/repo.git/
git clone rsync://example.com/path/to/repo.git/

SSH协议还有另一种写法

git clone [user@]example.com:path/to/repo.git/

branch命令

git branch newBranch            // 新建本地分支名称为newBranch
git checkout newBranch          // 切换到newBranch分支
git checkout -b newBranch         // 创建并切换分支
git checkout -b newBrach origin/master // 创建一个新的分支
git branch -d newBranch         // 删除本地分支newBranch
git branch -D newBranch         // 强制删除本地分支newBranch
git branch -v                    // 查看各个分支最后一次提交
git branch -a                   // 查看所有分支(本地分支+远程分支)
git branch -r                   // 查看远程分支
git branch -d -r origin/master2    // 删除远程分支feature_version2

其他命令

git fetch origin            // 更新远程库到本地
git fetch origin master  // 更新远程库的master分支到本地
git pull origin master   // Merge master分支代码到当前分支
git push origin newBranch
git push -u --all origin    // 将本地的所有分支都推送到远程主机
git rebase newBranch        // 更新mybranch分支上的东西到master上
git commit -a              // 提交
git reset HEAD^          // commit之后,如果想撤销最近一次提交(即退回到上一次版本)并本地保留代码
git add -A                  // 提交所有变化
git add -u                  // 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new)
git add .                  // 提交新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件
touch RRADME.md          // 创建md文件

案列一、创建一个新的仓库

git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/lsyz0021/temp.git
git push -u origin master

案列二、push到一个已存在的远程仓库

git remote add origin https://github.com/lsyz0021/temp.git
git push -u origin master

案列三、将已存在的仓库代码同步到新建的仓库中

git clone https://github.com/lsyz0021/temp.git // 克隆新建远程仓库
git remote add oldOrigin https://github.com/lsyz/EventBus.git // 给当前的仓库添加另一个远程仓库
// checkout所有oldOrigin远程的分支
git checkout -b master oldOrigin/master
git checkout -b master2 oldOrigin.master2
git push -u --all origin // push本地所有的分支到新的仓库(每个仓库默认会有一个origin)
git push -u --tags origin // push本地所有的tag到新的仓库

拿出微信 扫码关注下面的微信订阅号,及时获取更多推送文章

猜你喜欢

转载自blog.csdn.net/lsyz0021/article/details/72530673