GIT指令

64 阅读3分钟

image (1).png

安装git

配置用户名和邮箱

// 配置用户名和邮箱
git config --global user.name "your name"   # user.name后面是空格!
git config --global user.email "your email"

// 查看配置信息
git config --list

常用git指令

  1. 初始化仓库
    • git init
    • 在当前目录下创建一个新的Git仓库。
  2. 克隆仓库
    • git clone <repository>
    • 克隆远程仓库到本地。
  3. 添加文件到暂存区
    • git add <file>
    • 将文件添加到 Git 的暂存区,准备进行提交。
    • git add .git add -A
    • 将当前目录下的所有文件和子目录都添加到暂存区。
    • git add -u
    • 将所有已修改(但未被删除)的文件添加到暂存区。
  4. 提交更改
    • git commit -m "commit message"
    • 将暂存区的更改提交到本地仓库,-m 选项用于指定提交信息。
  5. 查看仓库状态
    • git status
    • 查看当前仓库的文件状态,包括已修改、已暂存或未跟踪的文件。
  6. 简化的看文件信息
    • git status -s
  7. 拉取远程仓库的更新
    • git pull
  8. 推送本地提交到远程仓库
  • git push
  1. 切换分支
  • git branch <new-branch>
  1. 合并分支
    • git merge <branch>
  2. 查看当前仓库所有分支
    • git branch
  3. 撤销修改
    • git restore <file>
  4. 查看历史信息
    • git log -n
    • git log --oneline
    # 按时间先后顺序列出所有的提交历史,最近的提交在最上面
    git log
    
    # 只展示最新的两条提交历史,数字可以按需进行填写
    git log -2
    
    # 一行显示 ,hash简写
    git log --oneline 
    
    # 在一行上展示最近两条提交历史的信息
    git log -2 --pretty=oneline
    
    # 在一行上展示最近两条提交历史信息,并自定义输出的格式
    # &h 提交的简写哈希值  %an 作者名字  %ar 作者修订日志  %s 提交说明
    git log -2 --pretty=format:"%h | %an | %ar | %s"
    
  5. 版本回退
  • git reset --hard <CommitID>
# 在一行上展示所有的提交历史
 git log --oneline

 # 使用 git reset --hard 命令,根据指定的提交 ID 回退到指定版本
 git reset --hard <CommitID>

 # 可以查看所有的版本记录
 git reflog 

组合指令

  1. 与远程仓库进行关联绑定
  • git remote add origin <remote_repository_url>
  1. 查看已添加的远程仓库列表
  • git remote -v
  1. 将当前本地分支的提交推送到名为 <branch_name> 的远程分支,并将二者进行关联。
    • git push -u origin <branch_name>
    • git push --set-upstream origin <branch_name>
  2. 将新创建的本地分支推送到远程仓库
    • git push -u origin <branch-name>
  3. 创建新分支并切换到该分支
    • git checkout -b <branch_name>
  4. 解决git status中文显示问题
    • git config --global core.quotepath false
  5. 跳过使用暂存区
    • 已经提交过的文件修改了,可以直接跳过暂存区,提交到仓库
    • git commit -a -m "提交"
    • git commit -am "update"
  6. 删除分支
    • git branch -d <branch_name>
    • 删除某个分支时不能在该分支上

文件状态

  • 未跟踪的文件 指的是,在git仓库目录下,新建的那些文件,一开始都是未跟踪状态
  • 已跟踪的文件 是指那些被纳入了版本控制的文件,(已经被git add .添加过)
1. Untracked 未跟踪

2. 已跟踪
- Unmodify 未修改    ==> 文件从git暂存区,提交到了仓库,文件和仓库中的代码一致
- Modified 已修改
- Staged 已暂存

如何生成ssh-key秘钥

# 1. 生成密钥对
ssh-keygen -t ed25519 -C "xxxxx@xxxxx.com"  
# Generating public/private ed25519 key pair...

# 2.查看公钥,在平台上配置公钥

cat ~/.ssh/id_ed25519.pub
# ssh-ed25519 AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc....


# 3. 检验是否本地和远端可以通讯,是否配置好(这个例子是gitee)
ssh -T git@gitee.com