GIT常用指令小记

关于GIT (更新中)

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

GITApache 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常用的设置有HTTPSSSH俩种。
###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来访问

Windows 10: 生成ssh key

需要按装:OpenSSH Client

管理员模式打开Terminal(Administrator: Command Prompt

以下我选择了默认路径+无密码:

C:\WINDOWS\system32>ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\UserName/.ssh/id_rsa):
Created directory 'C:\Users\UserName/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\UserName/.ssh/id_rsa.
Your public key has been saved in C:\Users\UserName/.ssh/id_rsa.pub.
The key fingerprint is:
......

使用SSH版本链接clone即可:

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

入门级

基础指令

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 (2020起, squash不再支持, 改用一下方式实现)

    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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值