User Tools

Site Tools


Sidebar

Go Back

Refresh

You are not allowed to add pages

Direct Link

library:git:generalcmd

This is an old revision of the document!


Git 常用命令


收集常用Git命令。

存取操作命令


  • 检出项目
git clone  git://192.168.0.1/xinsi/repo.git
git clone -b dev http://192.168.0.1/xinsi/repo.git
  • 提交
git add -u
git commit -m "Some message"
git push
  • 提取更新
git pull

分支(branch)操作命令


  • 查看分支状态
git branch
git branch -a
git branch -v
git branch -r
  • 建立分支 指定关联分支
git checkout -b dev
git branch --set-upstream-to origin/远程分支名  本地分支名
  • 指定关联分支
git branch --set-upstream-to=origin/<branch> you_branch
git branch --set-upstream-to origin/远程分支名  本地分支名
  • 本地分支改名
git branch -m old_branch new_branch
  • 切换分支
git checkout dev
  • 提交分支
git push --set-upstream origin dev
  • 删除本地分支
git branch -d <BranchName>
  • 删除远程分支
git push origin --delete dev
  • 合并分支
git checkout master
git merge dev
  • 拉取远程分支(已存在的)并创建本地分支
git checkout -b 本地分支名 origin/远程分支名

创建新分支

git checkout -b dev    # Create local branch "dev"
git push origin dev  # Push branch "dev" to remote

检出远程分支

git branch -a     # List all branchs on local and remote
git checkout dev  # Checkout branch "dev"

放弃本地修改


  • 未提交的,恢复到原来状态
git checkout .
  • 未提交的暂存到stash
git stash
  • 从stash恢复
git stash pop
  • 返回指定节点,不保留修改
git reset --hard HASH
  • 返回指定节点,保留修改
git reset --soft HASH
  • 放弃本地修改,重置为服务端版本
git fetch --all
git reset --hard origin/master
其中最后的master可以修改为其他branch

撤消操作


  • 撤消提交操作,重新提交
git commit -m 'initial commit'
git add forgotten_file
git commit --amend
  • 取消修改,回到之前的状态
// files
git reset HEAD <filename>

// all
git reset --hard
git pull

提交历史


git log

存储密码


清除密码

git config --system --unset credential.helper
or
git config --global credential.helper reset

保存用户名和密码

git config --global credential.helper store

设置默认用户名称和邮箱

git config --global user.name "your name"
git config --global user.email "hour mail box"

代码打包


git archive -o ../xxx.zip HEAD

显示中文


git config --global core.quotepath false

在 git log 时中文依然不能显示,首先试试用

git --no-pager log 

能不能显示中文,如果可以,则设置pager为more:

git config --global core.pager more 

以及,其他的一些解决办法: 进入你的项目根目录

1.设置git gui的界面编码

git config --global gui.encoding utf-8

2.设置 commit log 提交时使用 utf-8 编码,可避免服务器上乱码,同时与linux上的提交保持一致!

git config --global i18n.commitencoding utf-8
git config --global i18n.logoutputencoding utf-8

注:

windows系统默认编码为gbk,可改成gbk

如果系统设置了:

export LANG=zh_CN.UTF-8

则日志输出编码设置为utf-8

git config --global i18n.logoutputencoding utf-8

3.在 /etc/profile 中添加:

export LESSCHARSET=utf-8

在试一下问题解决了!

✔ 强制覆盖本地文件


git fetch --all

git reset --hard origin/master          # 如果在主分支上
or
git reset --hard origin/<branch_name>   # 如果在非主分支上

说明:
git fetch从远程下载最新的,而不尝试合并或rebase任何东西。
然后git reset将主分支重置为您刚刚获取的内容。 --hard选项更改工作树中的所有文件以匹配origin/master中的文件。

标签

列出标签
git tag
新建标签
git tag 标签名 -m "信息"

✔ 创建新分支

git branch -b dev    # Create local branch "dev"
git push origin dev  # Push branch "dev" to remote

✔检出远程分支

git checkout dev

✔修改Commit信息

commit后未push,修改信息命令:
git commit --amend

KEY

ssh-keygen -t rsa -C "youremail@example.com"

关掉自动转换功能

针对警告:
warning: LF will be replaced by CRLF in xxxxxxxx.
The file will have its original line endings in your working directory.

git config core.autocrlf false (仅对当前git仓库有效)
git config --global core.autocrlf false (全局有效)

临时保存

备份当前的工作区的内容
git stash save "comment"

查看临时列表
git stash list

查看修改
git stash show stash{0}

清除所有缓存的stash
git stash clear

CRLF与LF的转换

提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true   

提交时转换为LF,检出时不转换
git config --global core.autocrlf input   

提交检出均不转换
git config --global core.autocrlf false
library/git/generalcmd.1651422725.txt.gz · Last modified: 2022/05/02 00:32 by 127.0.0.1