Git常用命令详解

Git 是一个功能强大的版本控制系统,以下是一些常用的 Git 命令及其解释和案例:


1、配置

git config --global user.name "Your Name":配置全局用户名。
git config --global user.email "email@example.com:配置全局邮箱。

2、基础操作

初始化一个新的 Git 仓库。

  git init my_project

  cd my_project

git clone <repository>:克隆一个远程仓库。

git clone https://github.com/user/repo.git

3、文件操作

git add <file>:将文件更改添加到暂存区。

git add README.md

git status:查看当前仓库的状态。

git commit -m "Commit message":提交更改。

git commit -m "Add README.md"

4、分支管理

git branch:列出所有分支。

git branch <branchname>:创建新分支。
 
git branch feature/newfeature
  
git checkout <branchname>:切换分支。
 
git checkout feature/newfeature
  
git merge <branch>:将指定分支合并到当前分支。

git merge feature/newfeature

5、远程操作

git remote add <name> <url>:添加远程仓库。
 
git remote add origin https://github.com/user/repo.git
  
git push <remote> <branch>:推送分支到远程仓库。
 
git push origin master
  
git pull <remote> <branch>:从远程仓库拉取并合并。
 
git pull origin master

6、 查看历史

 git log:查看提交历史。

 git log oneline:查看简洁的提交历史。

7、撤销更改

git reset <commit>:回滚到指定提交。
 
git reset HEAD~1  # 回滚到上一个提交
  
git revert <commit>:创建一个新的提交来撤销某个提交的更改。
 
git revert HEAD  # 撤销最新提交
  
git rm <file>:删除文件。
 
git rm README.md

git commit m "Remove README.md"

 8、标签

git tag <tagname>:创建一个新的标签。

 
git tag v1.0

  
git push <remote> <tagname>:推送标签到远程仓库。

 
git push origin v1.0

以上只是一部分 Git 命令的概览。Git 非常强大,有着丰富的命令和选项,可以根据需要进行深入学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值