GIT常用指令小记

个人博客的中文小记,英文版:git-notes

GIT和Apache Subversion属于常见的代码版本控制的工具,本帖小记GIT常用指令已作参考。

安装 GitHub Desktop

GitHub for Windows

https://windows.github.com

GitHub for Mac

https://mac.github.com

Git for All Platforms

http://git-scm.com

配置

GIT常用的设置有HTTPS和SSH俩种。
###HTTPS
通过账号ID和密码访问

为了便于日常使用, 账号信息可配置为全局变量:

    git config --global user.name "[user-name]"
    git config --global user.email "[email address]"
    git config --global color.ui auto

###SSH
通过预设的SSH keys来访问

入门级

基础指令

Init

git init [project-name]

Clone (GIT HUB HTTPS)

    git clone https://github.com/[user-name]/[project-name]

Status Check up

    git remote -v # Check URL
    git status # Check local statue
    git diff
    git diff --staged

Stash: 缓存本地代码

    git stash
    git stash pop
    git stash list
    git stash drop

FILE

文件修改之后需要通过commit来提交修改:

    git commit "[descriptive message]"
    git push remote origin/[branch-name]

Add

    git add [file]

Remove

删除单文件

    git rm [file]

从GIT删除单文件,但保留本地缓存

    git rm --cached [file]

Modify

Reset single file

    git reset [file]

Rename single file

    git mv [file-original] [file-renamed]

BRANCH

查看本地Branch

    git branch

查看远程Branch

    git branch -r

建立新本地Branch

    git branch [branch-name]

与远程Branch同步

    git pull origin/[branch-name]

切换Branch

    git fetch
    git checkout [branch-name]

合并Branch到当前Branch (可能会导致冲突, 需要修改删除所有被 <<< 指出的冲突代码)

    git merge origin/[branch-name]

提交Branch到远程

    git push origin/[branch-name]

删除本地Branch

    git branch -d [branch-name]

Rebase: reapply commits on top of another base tip

    git rebase -i
    git rebase master [branch-name] # rebase branch to top of master

History check up

    git log
    git log --follow [file]
    git diff [first-branch]...[second-branch]
    git show [commit]

中级 (含强制执行指令,会导致代码永久丢失,新手慎用)

commit

撤销commit

撤销到指定commit:

    git reset [commit]

强制撤销到指定commit

    git reset --hard [commit]

撤回上个commit的修改

    git reset HEAD~
    git add [file-name] # Modify changes
    git commit -c ORIG_HEAD

Squash: 合并多个commits (Since 2020 squash it not neede, use reset --soft to do the same)

    git reset --soft HEAD~N # merge to last N commit
    git commit "[descriptive message]"
    git push -f origin [branch-name]

选用远程代码

    git reset –hard origin/[branch-name]

清理全部本地修改

    git clean -df
 
posted @ 2021-08-17 11:38  robinali  阅读(44)  评论(0编辑  收藏  举报