0%

Git中频命令速查

常用的clone、status、add、push、checkout等命令经常用不容易忘记,但是一些中等频率的命令每次都要查询,索性做个记录。完整的常用命令见附录。

设置用户信息

1
2
3
4
git config --global user.name "your_name"
git config --global user.email "your_name@abc.com"
#查看当前所有的设置
git config --list

新建工程

  • 本地有文件

    1
    2
    3
    4
    5
    6
    cd existing_folder
    git init
    git remote add origin [url]
    git add .
    git commit -m "Initial commit"
    git push -u origin master
  • 本地无文件

    1
    2
    3
    4
    5
    6
    git clone [url]
    cd testGit
    touch README.md
    git add README.md
    git commit -m "add README"
    git push -u origin master
  • 本地存在仓库

    1
    2
    3
    4
    5
    6
    cd existing_repo
    git remote rename origin old-origin
    git remote add origin [url]
    # 也可以直接修改: git remote set-url origin [url]
    git push -u origin --all
    git push -u origin --tags

分支、Tag相关

1
2
3
4
5
6
7
# 删除远程分支
git push origin --delete [branch-name]

删除本地tag:
git tag -d tagName
删除远程tag:
git push origin :refs/tags/tagName

shallow clone

clone大型仓库的最近N次提交

1
2
3
4
5
6
7
8
9
10
11
12
git clone --depth 1 [url]
# 取回tag
git fetch --tags

# 切换分支
git remote set-branches origin 'remote_branch_name'
git fetch --depth 1 origin remote_branch_name
git checkout remote_branch_name

# 取回完整仓库
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
git fetch --unshallow

附录