Git Common Usage

Common Commands

git restore --staged file # move file from staging area to working tree

git config --global user.name ""
git config --global user.email ""

git reset --hard ^HEAD # 版本回退
git reset --soft HEAD^ # come back last commit, don't delete commit content
git checkout -- [file] # 撤销修改
git stash # 暂存修改
git stash apply # 恢复修改

git remote set-url origin [url of the actual repo] # Change The URL Of Your Origin

git clone [email protected]:USER-NAME/REPOSITORY-NAME.git or git clone https://github.com/user-name/repository-name.git
git push origin master

git add --all
git commit -m "A message describing what you have done to make this snapshot different"

git status
git log

git branch -m master main # rename master to main
git remote rename origin upstream # rename `origin` remote to `upstream`

git show <commitid>:filename # see a file at a specific commit

git help -g

git show
# or
git log -n1 -p

git format-patch HEAD^

ref:

  1. https://github.com/k88hudson/git-flight-rules

The basic Git syntax is

program | action | destination

For example,

  • git add . is read as git | add | ., where the period represents everything in the current directory;

  • git commit -m "message" is read as git | commit -m | "message";

  • git status is read as git | status | (no destination).


A broader reference sheet from GitHub provideing quick instructions for using common commands can be found https://training.github.com/downloads/github-git-cheat-sheet/

多人合作开发

如果要开发多人合作项目,我们建议将 main 分支设置为 protected 分支,使得不允许直接在 main 上提交代码,只能通过 PR 的形式来合并。如何向项目提交 PR 请参考 GitHub 的 Pull Request 是指什么意思?

修改密码

$ git config user.name ""

然后,当我们再输入 git pull 等命令行时,就会被要求重新输入新的 账号密码。此时,密码就可以修改成功了。最后,我们还要输入如下命令,还原当前仓库的用户名:

$ git config user.name "username"

修改已经 push 的某次 commit 的作者/邮箱

已经 push 的记录,如果要修改作者信息的话,只能通过 –force 命令。

下面是来自 https://stackoverflow.com/a/750182/12539782 的脚本:

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then

    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"

fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then

    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    
fi
' --tag-name-filter cat -- --branches --tags

其他方法:

$ git filter-branch -f --env-filter \
"GIT_AUTHOR_NAME='Newname'; GIT_AUTHOR_EMAIL='newemail'; \
GIT_COMMITTER_NAME='committed-name'; GIT_COMMITTER_EMAIL='committed-email';" HEAD
# https://stackoverflow.com/a/2920001/12539782

修改 author

$ git rebase -i HEAD~n # 表示要修改前 n 次所有的提交

输入此命令后,显示以下结果:

pick ac0fcc6 add file2
pick a0cbfbe add file3
pick 16ee6eb add file4

# Rebase d57f11f..16ee6eb onto d57f11f (3 command(s))
#
# 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

我们要修改第二行和第三行的提交,根据提示,因此把第二行和第三行的 pick 改成 edit 或 e,保存退出。

保存上面的修改并退出后,git 会依次执行上面的操作,当操作为 pick 时,直接 commit。当操作为 edit 时,会中断,并提示以下信息:

You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

这里的意思是说,你可以使用 git commit --amend 来修改此次提交,修改以后,觉得满意了,执行 git rebase --continue 继续剩下的流程。

由于我们的主要目的是修改提交者的信息,因此光用 git commit --amend 是不够的,我们要使用 git commit --amend --author "yourname <your email>" 这样的操作,这一点是修改提交者信息的关键所在。

使用上面的命令成功修改此次提交的提交者信息后,一定要记得执行 git rebase --continue 继续。

最终完成以后提示如下:

$ git rebase --continue
Successfully rebased and updated refs/heads/main.

相关讨论:

  1. https://stackoverflow.com/q/750172/12539782

  2. git 修改已提交的某一次的邮箱和用户信息 https://segmentfault.com/q/1010000006999861

  3. 修改 git repo 历史提交的 author http://baurine.github.io/2015/08/22/git_update_author.html

branch1 的某个 commit1 合并到 branch2 当中

切换到 branch2 中,然后执行如下命令:

$ git cherry-pick commit1