User Tools

Site Tools


Sidebar

Go Back

Refresh

You are not allowed to add pages

Direct Link

library:git:generalcmd

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

临时保存和恢复工作区

git stash

git stash list
git stash pop = git stash apply + git stash drop

1. sub module

1.1 相关文件

  • .gitsubmodules

记录所有子模块的文件

[submodule "app/pm2"]
        path = app/pm2
        url = git@codeup.aliyun.com:xs/webapp/app.pm2.git
  • .git/config

[submodule “apps/pm2”]

        url = git@codeup.aliyun.com:xs/webapp/app.pm2.git
        active = true
        
 * .git/modules/<color #ed1c24>app/pm2</color>
 子模块仓库的副本,存放子模块仓库本身的.git目录文件
  • 子模块目录的 .git

指向主仓库记录子模块的路径

gitdir: ../../.git/modules/<color #ed1c24>app/pm2</color>

1.2 查看所有子模块

git submodule

1.3 克隆一个带子模块的仓库

  1. 下载主项目: git clone <remote-URL>
  2. git submodule init
  3. git submodule update
  4. 更新子模块: (在子模块目录) git pull origin master

注:

  • git submodule init git submodule update 两条指令可以合并为: git submodule update –init –recursive
  • git submodule init 的作用就是将子模块的信息写到 .git/config中
  • git submodule update 的作用是将子模块的空目录 clone 成对应版本的 git 仓库。
  • 当目录为空时,git submodule 显示的子模块不带版本信息。

1.4 主仓库添加子模块(已存在的git仓库)

git sumodule add <remote-URL> [name]
git add name
git commit -m "add submodule"
git push origin master

1.5 主仓库添加子模块(将已有的目录设为子模块)

  • 为sub-module创建一个单独的git仓库
git checkout v2.0
cd sub-module
git init
git add sub.txt
git commit -m "version 1.0 of sub-module"
git remote add origin https://github.com/bitmingw/sub-module.git
git push -u origin master
  • 从 main-module.git中删除 sub-module文件夹
cd .. # main-module/
git rm -r sub-module
git commit -m "remove sub-module directory"
  • 将 sub-module.git注册为main-module.git的子模块
main-module/
git submodule add https://github.com/bitmingw/sub-module.git
git commit -m "add submodule version 1.0"

1.6 删除子模块

  • 逆初始化模块,其中{MOD_NAME}为模块目录,执行后可发现模块目录被清空
 git submodule deinit <Submodule>
  • 删除.gitmodules中记录的模块信息(–cached选项清除.git/modules中的缓存)
git rm --cached <Submodule>
  • 提交更改到代码库,可观察到'.gitmodules'内容发生变更
git commit -am "Remove a submodule." 

1.7 向下同步子模块仓库

git submodule init
git submodule update

git submodule update --init --recursive
library/git/generalcmd.txt · Last modified: 2023/02/16 17:13 by lhaosen