git基础命令

git基础命令

初始化配置

git --version  :查看版本

git update-git-for-windows :更新版本

git config --global user.name XXX:配置用户名 

git config --global user.email XXXX:配置邮箱

git config --list :查看设置信息

git init :初始化仓库,将自己的项目git管理

git  status :查看工作目录中文件的状态(已跟踪(已提交 已暂存 已修改) 和未跟踪两种状态)  

git  log --oneline : 查看提交记录,一行显示

git diff : 查看未暂存的修改
git diff --cache : 查看未提交的暂存

新增或修改文件
git status
git add ./   :添加文件到暂存区,标记为已追踪状态
git commit -m "提交文件的相关注释"

删除文件
git rm 要删除的文件 
git status 
git commit -m "提交文件的相关注释"

重命名文件

git mv 老文件 新文件

git status

git commit -m "提交文件的相关注释"

分支相关:

git branch  :查看分支列表

 git branch name :创建新的分支

git checkout name: 切换分支

git branch -d name:删除空的分支 删除已经被合并的分支

git branch -D name : 强制删除分支

git checkout  -b name:创建并切换到新的分支

注意点:切换分支时,要保证当前分支处于干净状态,否则会污染到其他分支

使用git status
$ git status
On branch tets
nothing to commit, working tree clean

错误:当我们切换到其他分支时,当前分支还有一些未提交的文件

error: Your local changes to the following files would be overwritten by checkout: xxxx
Please commit your changes or stash them before you switch branches.
Aborting

解决:暂时存储到栈中

git  stash  : 会将当前分支上的工作内容推到一个栈中,即可切换到其他分支处理工作,返回当前分支时,执行git stash pop命令

git stash  list: 查看存储在栈中的工作内容列表

git stash pop:出栈并栈顶删除

远程操作:

git clone url :下载远程仓库到本地(默认克隆时为远程仓库起的别名为 origin)

git push  仓库名(默认为origin)  分支名

git merge  分支名:合并分支

git  fetch  仓库名  分支名:会访问远程仓库,从中拉取所有你还没有的数据,git merge  仓库名  分支名 合并代码

git pull :git fetch + git merge 一步到位,可能产生冲突。

原文地址:https://www.cnblogs.com/tdyang/p/12768368.html