git 常用命令

329 阅读3分钟

摘要:本文主要介绍git的常见使用方式,让大家在生产过程中能够快速应用查阅,节约大家的时间。

git介绍

所有命令集合还是看https://www.runoob.com/git/git-tutorial.html

基础命令

git config配置管理

  • git config --global user.name "xxx" 配置用户名
  • git config --global user.email "xxx" 配置邮箱
  • git config --list 查看配置列表
  • git config --global credential.helper store 长期保存密码

该方式用于http方法拉取的仓库

git clone代码克隆

  • git clone http://192.168.137.100:3001/huzhihui/demo-test.git
$ git clone http://192.168.137.100:3001/huzhihui/demo-test.git
Cloning into 'demo-test'...
warning: You appear to have cloned an empty repository.

git init初始化本地仓库

  • 运行 git init 表示在当前目录创建git版本管理。
  • 运行 git init a文件夹表示在当前文件夹下创建一个a文件夹做为版本管理空间。
$ git init demo-test
Initialized empty Git repository in L:/tt/demo-test/.git/

特别注意当前情况下默认是master分支,如果切换为其他分支需要使用git checkout -b 其他分支命令。

git remote远端仓库关联管理

适合用于本地项目已经存在,远端仓库是空的则用该命令很合适

  • git remote add origin http://192.168.137.100:3001/huzhihui/demo-test.git

git add

  • git add README.md添加制定文件
  • git add .添加全部变动文件

git rm

就是删除文件,和硬盘删除一样的

  • git rm hello.txt

git commit

提交变动的文件

  • git commit -m "新需求"

git push

  • 一般需要手动设置远端upstream,如git push --set-upstream origin master才能直接使用git push
  • 运行git push -f强制推送并覆盖远程分支代码

git pull

拉取远程代码

$ git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 180 bytes | 1024 bytes/s, done.
From http://192.168.137.100:3001/huzhihui/demo-test
 * [new branch]      ccc        -> origin/ccc
Already up to date.

git status

查看当前的版本状态

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    hello.txt

.gitignore文件忽略制定内容

如下内容

.vscode/
.factorypath
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear

git branch分支命令

  • 运行git branch查看当前所有分支
  • 运行git branch 分支名称创建新分支
  • 运行git branch -d 分支名称删除分支

git checkout切换分支

  • 运行git checkout 分支名称切换分支
  • 运行git checkout -b 分支名称创建+切换分支

git merge合并分支

  • 运行git merge 分支名称合并<分支名称>到当前分支

git stash改动文件暂存

  • git stash list查看暂存列表
  • git stash pop --index 0去除第0个暂存内容并删除暂存的数据
  • git stash save -m "临时保存"暂时保存一下改动文件

git log查看提交记录

推荐使用工具看,这个命令太麻烦了,自己用git log --help查看吧

  • git log --oneline查看简洁模式一行显示
  • git log --oneline --all显示所有分支的提交记录

git reset回退提交

这个命令我一般用--hard模式,下面的案例用的是--mixed

  • --hard模式:丢弃commit提交之后的文件修改
  • --mixed模式:重置提交索引到制定的提交,但是工作索引文件全部保留为未提交的文件。
$ git log --oneline
1ced087 (HEAD -> master, origin/master) four
c1f3042 three
81cb9e9 two
f74f5d5 init

$ git reset 81cb9e9

$ git status
On branch master
Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        3.txt
        4.txt

nothing added to commit but untracked files present (use "git add" to track)

# 强制覆盖远端版本
$ git push -f

实际场景组合用法

新项目流程

touch README.md
git init

git add README.md
git commit -m "first commit"
git remote add origin ssh://git@192.168.137.100:222/huzhihui/demo-01.git
git push -u origin master

正常提交文件

git add README.md
git commit -m "update readme"
git push

创建新分支并推送到远端仓库

git checkout -b feature/01
echo "hello" > hello.txt
git add hello.txt
git commit -m "hello"
git push feature/01

分支提交错了回退提交

$ git log --oneline
df596f8 (HEAD -> master, origin/master) 4提交
dd4cc84 3提交
81cb9e9 two
f74f5d5 init

$ git reset --hard 81cb9e9
HEAD is now at 81cb9e9 two

$ git push -f
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To http://192.168.137.100:3001/huzhihui/demo-test.git
 + df596f8...81cb9e9 master -> master (forced update)
 
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

$ git log --oneline
81cb9e9 (HEAD -> master, origin/master) two
f74f5d5 init

从其他分支获取指定的提交到当前分支

推荐使用工具了,命令行太难了, git-scm.com/docs/git-ch…

  • git cherry-pick