Git的操作

    配置

    1. # 显示当前的Git配置
    2. $ git config --list
    3. # 编辑Git配置文件
    4. $ git config -e [--global]
    5. # 设置提交代码时的用户信息
    6. $ git config [--global] user.name "[name]"
    7. $ git config [--global] user.email "[email address]"

    增加/删除文件

    1. # 添加指定文件到暂存区
    2. $ git add [file1] [file2] ...
    3. # 添加指定目录到暂存区,包括子目录
    4. $ git add [dir]
    5. # 添加当前目录的所有文件到暂存区
    6. $ git add .
    7. # 删除工作区文件,并且将这次删除放入暂存区
    8. $ git rm [file1] [file2] ...
    9. # 停止追踪指定文件,但该文件会保留在工作区
    10. $ git rm --cached [file]
    11. # 改名文件,并且将这个改名放入暂存区
    12. $ git mv [file-original] [file-renamed]

    分支

    1. # 列出所有本地分支
    2. $ git branch
    3. # 列出所有远程分支
    4. $ git branch -r
    5. # 列出所有本地分支和远程分支
    6. $ git branch -a
    7. # 新建一个分支,但依然停留在当前分支
    8. $ git branch [branch-name]
    9. # 新建一个分支,并切换到该分支
    10. $ git checkout -b [branch]
    11. # 新建一个分支,指向指定commit
    12. $ git branch [branch] [commit]
    13. $ git branch --track [branch] [remote-branch]
    14. # 切换到指定分支,并更新工作区
    15. $ git checkout [branch-name]
    16. # 建立追踪关系,在现有分支与指定的远程分支之间
    17. $ git branch --set-upstream [branch] [remote-branch]
    18. # 合并指定分支到当前分支
    19. $ git merge [branch]
    20. # 选择一个commit,合并进当前分支
    21. $ git cherry-pick [commit]
    22. # 删除分支
    23. $ git branch -d [branch-name]
    24. # 删除远程分支
    25. $ git push origin --delete <branch-name>
    26. $ git branch -dr <remote/branch>

    标签

    1. # 列出所有tag
    2. $ git tag
    3. # 新建一个tag在当前commit
    4. $ git tag [tag]
    5. # 新建一个tag在指定commit
    6. $ git tag [tag] [commit]
    7. # 查看tag信息
    8. $ git show [tag]
    9. # 提交指定tag
    10. $ git push [remote] [tag]
    11. # 提交所有tag
    12. $ git push [remote] --tags
    13. # 新建一个分支,指向某个tag
    14. $ git checkout -b [branch] [tag]

    远程同步

    1. $ git fetch [remote]
    2. # 显示所有远程仓库
    3. # 显示某个远程仓库的信息
    4. $ git remote show [remote]
    5. # 增加一个新的远程仓库,并命名
    6. $ git remote add [shortname] [url]
    7. # 取回远程仓库的变化,并与本地分支合并
    8. $ git pull [remote] [branch]
    9. # 上传本地指定分支到远程仓库
    10. $ git push [remote] [branch]
    11. # 强行推送当前分支到远程仓库,即使有冲突
    12. $ git push [remote] --force
    13. # 推送所有分支到远程仓库
    14. $ git push [remote] --all

    撤销

    1. # 恢复暂存区的指定文件到工作区
    2. $ git checkout [file]
    3. # 恢复某个commit的指定文件到工作区
    4. $ git checkout [commit] [file]
    5. # 恢复上一个commit的所有文件到工作区
    6. $ git checkout .
    7. # 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
    8. $ git reset [file]
    9. # 重置暂存区与工作区,与上一次commit保持一致
    10. $ git reset --hard
    11. # 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
    12. $ git reset [commit]
    13. # 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
    14. $ git reset --hard [commit]
    15. # 重置当前HEAD为指定commit,但保持暂存区和工作区不变
    16. $ git reset --keep [commit]
    17. # 新建一个commit,用来撤销指定commit

    (完)