Git 使用教程

拉取指定分支
# -b 后面跟分支名
git clone -b bych https://github.com/umrcheng/pyvm-windows.git

添加、修改、删除以及查看本地git的用户信息

全局

查看

git config --global user.name
git config --global user.email

修改

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

删除

git config --global --unset user.name "yourName"
git config --global --unset user.email "your@email.com"

查看全部信息

git config --list

本地(当前仓库)

全局的命令去掉 --global 即可变为当前仓库

其他

# 提示“warning: LF will be replaced by CRLF”的解决办法
git config --global core.autocrlf true

# 删除源
git remote remove origin

# 增加源
git remote add origin ‘address’
git remote set-url origin ‘address’

# 推送到指定分支 develop
git push origin develop

# 回滚
git reset --hard XXXXXXX回滚到分支

# 强制推送到master分支,获取其它分支,自己修改
git push -f origin master

添加文件,并推送

添加到暂存区

git add .
  • . 代表所有文件

查看状态

git status

提交到仓库

git commit -m "初始化仓库"
  • -m 为 为了添加到暂存区(git add) 命令添加的文件设置操作介绍

添加远程仓库地址

git remote add origin https://github.com/vuejs/vue.git

查看远程仓库

git remote -v
git remote show origin

推送

git push origin master
  • 将 commit 和 git add 的内容推送到仓库。
  • -u,使用-u选项指定一个默认主机。git push后面加上参数

移除暂存区中的文件

使用 git rm 命令 删除暂存区指定文件

git rm --cached <file>
  • 将暂存区中的内容删除,工作区中对应的文件并不会受到影响。

使用 git reset HEAD 命令 撤销已被放入暂存区的文件

撤销暂存区指定文件(取消某一个文件的缓存)

git reset HEAD <file>

撤销暂存区所有文件(取消 git add 缓存的所有内容)

git reset HEAD .

使用 git restore 命令

取消某一个文件的暂存

git restore --staged <file>

取消所有已暂存的文件

git restore --staged .
  • 此命令将会将文件从暂存区移除,但保留在工作区中。

2. git 配置每次 拉取推送 都要输入密码

git for windows 要打开 git bash 中输入,linux mac windows 命令行输入

git config --global --unset credential.helper
git config --global credential.helper ""

3. git 配置私人令牌可以避免每次 拉取推送 都要输入密码

# 仅在一段时间内被缓存,可使用 cache 凭证助手
git config --global credential.helper cache

# 密码会被缓存 15 分钟。你可以通过以下命令来调整缓存时间(以秒为单位),例如设置为 1 小时(3600 秒)
git config --global credential.helper 'cache --timeout=3600'

# 永久保存密码,可以使用 store 凭证助手
git config --global credential.helper store

4. git 配置 ssh公钥 避免每次 拉取推送 都要输入密码

查看当前分支

git branch

git branch --show-current

git 合并分支到主分支

# 切换到 master 分支
git checkout master

# 拉取 master 最新代码(避免冲突 可选)
git pull origin master

# 执行合并操作
git merge dev

# 推送合并结果到远程仓库
git push origin master

# 切换到 dev 开发分支
git checkout dev

解决可能的冲突

如果合并时出现冲突,需要手动编辑冲突文件
编辑完成后,标记为已解决:

git add <冲突文件路径>

git commit -m "Merge branch 'dev' into master"

四下皆无人