Skip to content

Instantly share code, notes, and snippets.

@songlairui
Created March 28, 2020 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songlairui/69528b3d74f6aff79cbaa1c6baff177d to your computer and use it in GitHub Desktop.
Save songlairui/69528b3d74f6aff79cbaa1c6baff177d to your computer and use it in GitHub Desktop.
git 常用命令

git 常用状态命令

分支对比 git rev-list

# 查询分支间 up and down
git rev-list --left-right ${base_commit}...${compare_commit}

# 支持传入 commit 和 分支名
git rev-list --left-right 1520533d110efae320801bde33770ac9fc1fee8a...origin/master

# 传入 --count 返回数字
git rev-list --left-right master...origin/master --count

查询分支状态

# git branch 查询
git branch -vv

# 查询当前分支 commit
git rev-parse HEAD
# 查询当前分支 名
git rev-parse --abbrev-ref HEAD

# 查询当前分支 upstream / tracking
# https://stackoverflow.com/questions/46514831/how-read-the-current-upstream-for-a-git-branch
git rev-parse --abbrev-ref master@{u}
const path = require('path')
const os = require('os')
const fs = require('fs')
const SimpleGit = require('simple-git/promise')
const dirs = fs.readFileSync(path.join(__dirname, './repo-dirs.txt')).toString().split('\n')
async function main() {
for (const dir of dirs) {
const cwd = path.join(dir, '..')
const git = SimpleGit(cwd)
console.info(`\n~/${path.relative(os.homedir(),cwd)}`)
const result = await git.getRemotes(true)
if (result && result.length) {
console.info(` ${result.map(({name,refs:{fetch,push}})=>`${name} ${fetch === push ? `${fetch}` : `${fetch}${push}`}`).join('\n ')}`)
const currentBranch = await git.revparse(['--abbrev-ref', 'HEAD']).catch(e=>{
// console.info('current fail', e.message)
})
const remoteBranch = await git.revparse(['--abbrev-ref', `${currentBranch || 'master'}@{u}`]).catch(e=>{
// console.info('current fail', e.message)
})
console.info(' local -> remote', currentBranch,":::", remoteBranch)
if (currentBranch && remoteBranch) {
const diffCount = await git.raw(['rev-list','--left-right',`${currentBranch}...${remoteBranch}`, '--count']).catch(e=>{
// console.info('current fail', e.message)
})
console.info(' diffCount', diffCount.trim().split(/\s+/))
}
} else {
console.warn('\t【lack remote】')
}
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment