git命令使用

git命令

1、centos-install-git免密

#  安装
yum -y install git
# 生成公私钥
ssh-keygen -t rsa 

# 拷贝公钥到gitlab网页中配置-SSH-KEYS中

2、win-install-git免密

#windows下载地址:
https://github.com/git-for-windows/git/releases/download/v2.12.2.windows.2/MinGit-2.12.2.2-64-bit.zip    
#windows  打开 git Bash
ssh-keygen -t rsa -C “你的邮箱” 
系统盘用户目录会有一个 .ssh 目录。里面存放着公钥和私钥文件


#mac安装:
brew install git 

# 拷贝公钥到gitlab网页中配置-SSH-KEYS中

3、初始化本地代码库

#设置你的名称
git config --global user.name "XXX"
#设置你的邮箱
git config --global user.email "XXX"
#设置SSL忽略
git config --global http.sslVerify false
#克隆完整代码库
git clone https://gitlab/oma/doc.git

4、创建版本库

#  克隆远程版本库
git clone [url]

# 初始化本地版本库
git init

5、修改和提交

# 查看状态
git status

# 查看变更内容
git diff
git diff --cached 
git diff commitid:file1 commititd:file2

# 跟踪所有改动过的文件
git add .

# 跟踪指定的文件
git add <file\>

# 文件改名
git mv <old\> <new\>

# 删除文件
git rm <file\>

# 停止跟踪文件但不删除
git rm --cache <file\>

# 提交所有更新过的文件
git commit -m "commit message"

# 修改最后一次提交
git commit --amend

# 跳过缓冲区提交
git commit -a -m "commit message"

6、查看提交历史

# 查看提交历史
git log

# 查看指定文件的提交历史
git log -p <file\>

# 以列表方式查看指定文件的提交历史
git blame <file\>

7、撤销

# 撤销缓冲区域的文件
git reset HEAD <file\>

# 撤销工作目录中所有未提交的文件的修改内容
git reset --hard HEAD

# 撤销指定的未提交文件的修改内容
git checkout HEAD <file\>

# 撤销指定的提交
git revert <commit\>

# 取消对文件的修改
git checkout -- <file\>


8、分支与标签

# 显示所有本地分支
git branch

# 切换到指定分支或标签
git checkout <branch/tag/>

# 创建新分支
git branch <new-branch/>

# 删除本地分支
git branch -d <branch/>

# 列出所有本地标签
git tag

# 基于最新提交创建标签
git tab <tagname\>

# 删除标签
git tag -d <tagname\>

9、合并与衍合

# 合并指定分支到当前分支
git merge <branch\>

# 衍合指定分支到当前分支
git rebase <branch\>

10、远程操作

# 查看远程版本库信息
git remote -v 

# 查看指定远程版本库信息
git remote show <remote\>

# 添加远程版本库
git remote add <remote\> <url\>

# 从远程代码库获取代码
git fetch <remote\>

# 下载代码及快速合并
git pull <remote\> <branch\>

# 上传代码及快速合并
git push <remote\> <branch\>

# 删除远程分支或标签
git push <remote\> :<branch/tag-name\>

# 上传所有标签
git push --tags

11、其他

# 配置git命令补全
#找到补全shell
rpm -ql git | grep bash 
/etc/bash_completion.d
/etc/bash_completion.d/git
/usr/share/doc/git-1.7.1/contrib/completion/git-completion.bash

#拷贝补全bash
cp /usr/share/doc/git-1.7.1/contrib/completion/git-completion.bash ~/.git-completion.bash

#加载环境变量
source ~/.git-completion.bash




# 配置git别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status    
posted @ 2024-07-31 10:30  itk  阅读(34)  评论(0)    收藏  举报