# Git项目管理 ## 1. Git使用用户配置 ```bash git config --global user.name "wang" git config --global user.email "wangyuedong94@qq.com" # 查看git配置信息 git config --list ``` ## 2. Git基本命令 ```bash # 初始化本地仓库 git init # 查看仓库git状态 git status # 增加文件到缓存区 git add # 提交到本地仓库 git commit -m "Add file" # 查看本地仓库提交历史记录 git log git reflog # 查看提交记录图 git log --decorate --all --graph --oneline # 回退到之前版本 git reset --hard cef96r(版本编号) ``` ## 3. 创建和切换分支 ```bash # 创建分支v0.1 git branch v0.1 # 切换到分支v0.1 git checkout v0.1 # 合并分支,在主分支操作,合并前解决所有同名文件冲突 git merge v0.1 # 删除分支 git branch -d v0.1 # 删除远程分支 git branch -dr remote/branch git push origin --delete v0.1 # 查看分支 git branch # 查看所有分支 git branch -a # 查看远程分支 git branch -r ``` ## 4. 远程仓库 ```bash # 添加远程仓库, 可以存在多个远程仓库github, gitlab, gitee git remote add origin https://gitlab.com/(username)/(project).git #origin为远程仓库别名 # 推送远程仓库 git push origin master #git push [远程仓库名] [本地分支名] #git push [远程仓库名] [本地分支名]:[远程分支名] #git push origin master:test # 推送远程仓库加-u git push -u origin master # -u设置了默认上传流远程仓库 git push # 拉取远程仓库项目 git clone <远程仓库地址> # 拉取远程项目分支v0.1 git clone -b v0.1 <远程仓库地址> # 拉取远程分支 git fetch origin v0.1 # 拉取远程仓库更新 git pull origin master # 查看所有远程仓库 git remote # 设置新的远程仓库地址 git remote set-url origin https://gitlab.com/... # 删除远程仓库 git remote rm origin ``` ## 5.凭证助手 清除凭证助手 ```bash git config --system --unset credential.helper # 除了system外,还有global、local等范围 # 使用 git config --list 命令这是展示配置属性,只要不存在credential.helper表示清除成功 ``` 加认证测试 ```bash ssh -T git@gitee.com ``` 或者 ```bash git config --global credential.helper store # 清除成功后,每次远程操作pull/push/fetch时需要手动输入密码啊。 # 执行这个命令,开启凭证助手,一次输入密码认证成功后会被存储下来。 ```