本文收集了常用的Git命令,方便平时开发查询。
一、常用命令
git reset
- git log 找到上次git commit的 id
- git reset –hard commit_id
完成撤销,同时将代码恢复到前一commit_id 对应的版本。 - git reset commit_id
完成Commit命令的撤销,但是不对代码修改进行撤销,可以直接通过git commit 重新提交对本地代码的修改。
1 | git reset --hard 0530be1c55b53dc809321fda4ee6f613a245d356 |
git clone
1 | //最好用以下方法Clone单个分支: |
git checkout
1 | $git checkout branche_name |
git fetch
1 | //在本地新建一个dev分支,并将远程origin仓库的dev分支代码下载到本地dev分支 |
- Unstaged local changes (before you commit)
- Discard all local changes, but save them for possible re-use later. git stash.
- Discarding local changes (permanently) to a file. git checkout –
- Discard all local changes to all files permanently. git reset –hard.
git cherry-pick
cherry-pick会重演某些commit, 即把某些commit的更改重新执行一遍。
可以在当前分支上,cherry-pick其他分支上的commit
1 | $ git cherry-pick 0ed412c23ef5547c8045555966e8c8353cada173 |
git reset
撤销commit
1 | //注意:reset时候,不要有需要合并的代码,即之前未pull下来的。 |
git differ
比较两个版本代码
1 | // 比较commit_id_1和commit_id_1 |
二、ignore file
三、Error
Git push
git push error: RPC failed; curl 56 LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
处理方案:
方案一:
1 | git config --global http.postBuffer 524288000 |
方案二:
对于 errno 54 这个错误,经尝试 http 或者 https 协议都无法正常提交。
必须改为 ssh 方式来提交代码。也就是必须使用公私钥的方式进行账号验证,并提交代码。
具体生成公私钥的方法,参见 github 帮助文档
https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/
同时找到 git config 当中的项目 url 配置项,改为 ssh 协议类型的项目地址。
例如:url = git@github.com:xxx/xxx.git
Git clone
error: RPC failed; HTTP 504 curl 22 The requested URL returned error: 504 Gateway Time-out
fatal: The remote end hung up unexpectedly
1 | $ git clone --depth=1 https://git.xxxxxx/xxxxxx.git |