Git常用命令

记录一下Git的常用命令

Git常用的命令

标签(tag)

1
2
3
git tag 1.1.1 // 打标签
git tag -d 1.1.1 // 删除标签
git push origin --tags // 推送所有变化的标签

远程(remote)

1
2
3
git remote -v // 查看远程地址
git remote rm origin // 删除远程
git remote add origin https://github.com/momo13014/SDNetworking.git // 增加远程

Stash

1
2
3
4
git stash // 保存当前缓存
git stash pop // 取出当前缓存
git stash list // 查看缓存列表
git stash clear // 清除缓存标记列表

合并

1
2
3
git merge master // 将主分支合并到当前分支
git rebase master // 主分支和当前分支溯源合并(破坏git线路)
-- 其他git rebase --continue, git rebase --abort等命令

分支

1
2
3
4
5
git checkout -b new_branch_name // 新建分支
git branch -m another_branch_name // 更改当前分支的名字
git checkout master // 切换回主分支
git branch -D another_branch_name // 删除本地分支(不能处于被删除的分支上)
git push origin --delete origin/new_branch_name // 删除远程分支

回归

1
2
git reset --hard commit_id // 返回某个commit_id所对应的节点,并且删除所有更改
git reset --soft commit_id // 返回某个commit_id所对应的节点,但是在本地保留所有更改

? 如何找回已经 git reset –hard commit-id信息

1
git reflog show

得到以下信息:

1
2
3
4
5
8a1a4a3 (HEAD -> master) HEAD@{0}: reset: moving to 8a1a4a34611a9b2ccc962fc9109f23b2fd4707d3
cd03afe HEAD@{1}: commit: message one
8a1a4a3 (HEAD -> master) HEAD@{2}: rebase -i (finish): returning to refs/heads/master
8a1a4a3 (HEAD -> master) HEAD@{3}: rebase -i (reword): update readme again
其中 commit: message one 是被reset --hard的commit信息

1
git reset --hard cd03afe

即可找回commitId:cd03afe的信息

Commit

  1. 更改本地commit信息(本地已经commit了,尚未推送到远程)
    git commit --amend 打开最后一条commit信息
    然后在文本编辑器中保存, wq,然后git log就可以看到更改后的commit信息
  2. 更改远程的commit信息
    重复上面一步,先编辑完commit信息,然后强制更新到远程
    bash git push origin your_branch --force
  3. 更改更老的commit信息或者多条commit信息
    使用 git rebase -i HEAD~n命令,展示最后n条commit信息
    git rebase -i HEAD~2
    将会得到以下信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# pick d18086f change readmes
# pick 8a1a4a3 update readme again
# Rebase f55837b..8a1a4a3 onto f55837b (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

pick关键词替换成reword,关闭保存commit list file, 然后在每条打开的commit文件后修改并保存,
完成后使用 git log 就可以看到更改后的commit信息,然后强制推送到远程

参考链接:changing-a-commit-message

参考链接:

  1. Common git commands

  2. Git官网