git 常用命令

前言

整理一些自己常用的shell 脚本,主要是关于git的

让文件回到最近一次git commit或git add时的状态

(use “git checkout — <file>…” to discard changes in working directory)

devzkndeMacBook-Pro:taokesearchtweak devzkn$ git checkout --  Tweak.xm

ps:用命令git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区

devzkndeMacBook-Pro:guangyouqian devzkn$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   guangyouqian/Assets.xcassets/AppIcon.appiconset/Contents.json

devzkndeMacBook-Pro:guangyouqian devzkn$ git reset HEAD guangyouqian/Assets.xcassets/AppIcon.appiconset/Contents.json
Unstaged changes after reset:
M    guangyouqian/Assets.xcassets/AppIcon.appiconset/Contents.json
devzkndeMacBook-Pro:guangyouqian devzkn$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   guangyouqian/Assets.xcassets/AppIcon.appiconset/Contents.json

no changes added to commit (use "git add" and/or "git commit -a")

此时可以用 git checkout — 进行处理即可

devzkndeMacBook-Pro:guangyouqian devzkn$ git checkout -- *
devzkndeMacBook-Pro:guangyouqian devzkn$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
devzkndeMacBook-Pro:guangyouqian devzkn$ 

git 常用合并命令

切换回master分支

devzkndeMacBook-Pro:guangyouqian devzkn$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
devzkndeMacBook-Pro:guangyouqian devzkn$ git merge --no-ff  develop


devzkndeMacBook-Pro:guangyouqian devzkn$ git status

git tag -a v0.1.0 master
git push --tags

提交当前的修改代码并push到远程仓库的shell 脚本

#!/bin/sh
# dirname $0,取得当前执行的脚本文件的父目录
# cd `dirname $0`,进入这个目录(切换当前工作目录)

# cd `dirname $0` 
#alias gitadd='cd `dirname $0` && git add . && git commit -m /!* && git push'
git add .
git commit -m $1
git push
exit 0

用法:

devzkndeMacBook-Pro:weiliu devzkn$ kngit  批量上报列表

shell 脚本:在存在的项目中,创建git 仓库,并上传到远程仓库地址

devzkndeMacBook-Pro:bin devzkn$  kngitinit git@github.com:zhangkn/KNBin.git
Initialized empty Git repository in /Users/devzkn/bin/.git/
[master (root-commit) 879cd86] first commit
 6 files changed, 99 insertions(+)
 create mode 100644 README.md
 create mode 100755 class-dump
 create mode 100755 fixheader.sh
 create mode 100755 ios-class-dump
 create mode 100755 kngit
 create mode 100755 kngitinit
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 405.25 KiB | 816.00 KiB/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:zhangkn/KNBin.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
#!/bin/sh
# dirname $0,取得当前执行的脚本文件的父目录
# cd `dirname $0`,进入这个目录(切换当前工作目录)

# cd `dirname $0` 
#alias gitadd='cd `dirname $0` && git add . && git commit -m /!* && git push'
#!/bin/bash
#
# NOTE: You need to `brew install git` on Mac
# 
# by @kunnan
# https://github.com/zhangkn/KNAlipayWalletTweakDemo
# 在存在的项目中,创建git 仓库,并上传到远程仓库地址
#
# usage: kngitinit  SSHURL git@github.com:zhangkn/KNAlipayWalletTweakDemo.git


usage() {
  echo "在存在的项目中,创建git 仓库,并上传到远程仓库地址"
  echo
  echo "Usage: $0 {SSHURL}"
}

if [ ! -n "$1" ] ;then
  echo "Invalid url $1"
  usage
  exit 1
fi
# 变量赋值不要有空格
var=$1
##*/ 表示从左边开始删除最后(最右边)一个 / 号及左边的所有字符 即删除 git@github.com:zhangkn/ 结果是 KNAlipayWalletTweakDemo.git
var=${var##*/}
#%/* 表示从右边开始,删除第一个 / 号及右边的字符
var=${var%.*}
echo "# $var" >> README.md
#echo "# " >> README.md
git init
git add .
git commit -m "first commit"
git remote add origin $1
git push -u origin master
exit 0

比较的是工作目录(Working tree)和暂存区域快照(index)之间的差异

也就是修改之后还没有暂存起来的变化内容

devzkndeMacBook-Pro:weiliu devzkn$     git diff weiliu/weiliu.xm

b、查看已经暂存起来的文件(staged)和上次提交时的快照之间(HEAD)的差异

git diff --cached
git diff --staged

显示的是下一次commit时会提交到HEAD的内容(不带-a情况下)

c、显示工作版本(Working tree)和HEAD的差别

git diff HEAD

d、直接将两个分支上最新的提交做diff

git diff topic master 或 git diff topic..master

e、输出自topic和master分别开发以来,master分支上的changed。

git diff topic...master

Changes that occurred on the master branch since when the topic
branch was started off it
f、查看简单的diff结果,可以加上–stat参数

git diff --stat

g、查看当前目录和另外一个分支的差别

git diff test

显示当前目录和另一个叫’test’分支的差别

git diff HEAD -- ./lib

显示当前目录下的lib目录和上次提交之间的差别(更准确的说是在当前分支下)

h、比较上次提交commit和上上次提交

git diff HEAD^ HEAD

i、比较两个历史版本之间的差异

git diff SHA1 SHA2

补充

  • 删除 目录 以及目录内部的文件
rm -rf  .git
    原文作者:kunnan
    原文地址: https://segmentfault.com/a/1190000011731621
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞