关于git:GitCommon-Git-Command-Line-Operation

139次阅读

共计 6160 个字符,预计需要花费 16 分钟才能阅读完成。

Source

Common Git Command Line Operation | Chanvin’s Blog (chanvinxiao.com)

This article record the specific usage method of some common git command line operation

本文记录了一些罕用 git 命令行操作的具体应用办法

git clone

拉取 git 我的项目到本地。

  • git clone REPOSITORY_URL
    Clone repository, and use the name of repository as local folder name

    克隆版本库,并应用版本库名称作为本地文件夹名称

  • git clone REPOSITORY_URL FOLDER
    Clone repository, and use FOLDER as local folder name

    克隆存储库,并应用 FOLDER 作为本地文件夹名称

git fetch

  • git fetch origin
    Update all the remote branch

    更新所有近程分支

  • git fetch origin BRACH
    Update designated remote branch

    更新指定的近程分支

git pull

  • git pull origin
    Equivalent to fetch + merge coresponding upstream branch

    把分支推到远端对应的上游分支

  • git pull origin BRACH
    Pull designated branch to current branch

    等同于 fetch + merge 对应上游分支

  • git pull origin --rebase master
    Make local branch rebase remote master branch

    让本地分支重定向近程主分支

git push

  • git push origin
    Push branch to coresponding remote upstream branch

    将分支推送到对应的近程上游分支

  • git push origin BRANCH
    Push branch to remote designated branch

    将分支推送到近程指定分支

  • git push --set-upstream origin BRANCH
    Push branch to remote designated branch, and make it as upstream branch (generally need to be used for pushing branch of your own for the first time)

    将分支推送到近程指定分支,并使其成为上游分支(个别用于首次推送本人的分支)

  • git push -f origin
    Force push branch to corresponding remote upstream branch (will override remote branch, need to be used carefully)

    将分支推送到近程指定分支,并使其成为上游分支(个别用于首次推送本人的分支)

  • git push origin -d BRANCH
    Delete remote branch

    删除近程分支

git branch

  • git branch
    List all local branch

    删除近程分支

  • git branch -a
    List all local and remote branch

    列出本地和近程分支

  • git branch -m NEW_BRANCH
    Rename current branch

    重命名以后分支

  • git branch -d BRANCH
    Delete merged branch

    删除已合并的分支

  • git branch -D BRANCH
    Force delete branch (even if not be merged yet)

    强制删除分支(即便未合并)

git checkout

  • git checkout BRANCH
    Switch to designated branch

    切到对应分支

  • git checkout -b NEW_BRANCH
    Create new branch

    创立新分支

  • git checkout -b NEW_BRANCH BRANCH
    Create new branch based on BRANCH

    基于 BRANCH 创立新分支

  • git checkout SHA-1
    Switch to a commit, or use HEAD~N (N as 1, 2, 3…) to switch to previous Nth commit

    切换到某个提交,也能够用 HEAD~N(N 为 1, 2, 3…)切到上 N 个提交

  • git checkout SHA-1 /PATH/TO/FILE
    Restore file to designated commit version

    把文件还原到相应的提交版本

  • git checkout --theirs /PATH/TO/FILE
    Use theirs’file version in case of conflict

    有抵触时应用对方的文件版本

  • git checkout --ours /PATH/TO/FILE
    Use ours’file version in case of conflict

    有抵触时应用本人的文件版本

  • git checkout -
    Switch to previous branch, suitable for switching frequently between two branches

    切换到之前的分支,适宜在两个分支频繁切换时应用

git add

  • git add .
    Mark all added / modified / deleted files as to-be-committed

    把所有减少 / 批改 / 删除的文件标识为要提交

  • git add /PATH/TO/FILE
    Mark a single file as to-be-committed, suitable for situation when there’re other modified files which don’t need to be committed

    只把繁多文件标识为要提交,当有其余不须要提交的文件被批改时可应用

git commit

  • git commit
    Commit files marked by git add

    把 git add 标识的文件进行提交

  • git commit -a
    Commit modified / deleted files (if there’s newly added file, need to use git add to mark firstly)

    把批改 / 删除的文件进行提交(如果有新增的文件,须要应用 git add 增加)

  • git commit -am "MESSAGE"
    Commit modified / deleted files and assign comment (suitable for temporary or simple comment content)

    把批改 / 删除的文件进行提交并指定正文(实用于长期或简略正文内容)

  • git commit --amend
    Update last commit, can add -a or run git add firstly to append updated files

    更新上一次提交,能够加上 -a 或在之前运行 git add 追加更新文件

  • git commit --amend --reset-author
    Default updating commit won’t change author, can explicitly config if necessary

    默认的更新提交是不扭转作者的,如果须要扭转能够明确配置

git cherry-pick

  • git cherry-pick SHA-1
    Apply a commit to current branch

    把某个提交利用到以后分支

git status

  • git status
    Check current status

    查看目前状态

git diff

  • git diff
    Updating contents of current modified files which has not been marked as to-be-committed

    以后所有批改到的,没被标识为要提交的文件的更新内容

  • git diff --cache
    Updating contents of current modified files which has been marked as to-be-committed

    以后所有批改到的,并被标识为要提交的文件的更新内容

  • git diff /PATH/TO/FILE
    Updating contents of designated file, and can also be distinguished with --cache

    指定文件的更新内容,同样能够用 --cache 辨别

git log

  • git log
    Show all logs in detail

    具体显示所有记录

  • git log -n 10
    Show latest 10 logs

    显示最近 10 条记录

  • git log --oneline
    Show all logs briefly

    简要显示所有记录

  • git log --oneline master ^BRANCH | wc -l
    Compute how much commit differences between BRANCH and master

    能够计算 BRANCH 和 master 分支相差多少个提交

git stash

  • git stash
    Stash modified / deleted files, and the added files marked as to-be-committed

    暂存批改 / 删除,或已标识为要 commit 的新增的文件

  • git stash -u
    Stash modified / deleted / added files, which means the added files is included without using git add

    暂存批改 / 删除 / 新增的文件,即新增文件能够不必 git add

  • git stash pop
    Pop the stashed files

    把暂存的文件从新放进去

git revert

  • git revert SHA-1
    Cancel a commit by forming a new commit

    通过造成一个新提交勾销某个提交

  • git revert SHA-1 -m 1
    If the to-be-reverted commit is a merged one, need to designate which parent commit to be reverted to
    For example, if the merged commit is merging from BRANCH_2 to BRANCH_1, and you want to revert to BRANCH_1, then m should be 1 (it’s the most cases)

    如果是合并节点,须要指定要勾销提交对应的父节点,例如合并是把 BRANCH_2 合并到 BRANCH_1,那么要在 BRANCH_1 勾销这次合并,就应该指定 m 为 1(大多数状况都是这样)

git reset

  • git reset
    Cancel marking for to-be-committed files (equivalent to withdrawing git add)

    勾销对要 commit 的文件的标识(相当于 git add 的撤销)

  • git reset --hard
    Cancel updating for modified / deleted files and added files marked as to-be-committed

    勾销批改 / 删除或已标识为要 commit 的新增的文件的更新

  • git reset SHA-1
    Cancel all the commits after SHA-1, but retain updates of the committed files
    If want to just cancel last commit, SHA-1 can be set as HEAD^

    勾销从 SHA-1 之后的所有提交,然而保留提交文件的更新,如果只想勾销上一次提交,SHA-1 能够设为 HEAD^

  • git reset --hard SHA-1
    Cancel all the commits after SHA-1, and don’t retain updates of the committed files

    更新 SHA-1 当前的提交,能够 pick/pedit/edrop/dsquash/s 相应提交,如果第一个提交应用 p,前面的提交应用 s,能够把多个提交合并成一个提交

git rebase

  • git rebase BRANCH
    Make current branch rebase BRANCH

    让以后分支从新基于 BRANCH

  • git rebase -i SHA-1
    Update commits after SHA-1, can pick/pedit/edrop/dsquash/s corresponding commits
    If the first commit used with p, and the following commit used with s, then multiple commits will join into a single commit

    更新 SHA-1 当前的提交,能够 pick/pedit/edrop/dsquash/s 相应提交,如果第一个提交应用 p,前面的提交应用 s,能够把多个提交合并成一个提交

git merge

  • git merge BRANCH
    Merge BRANCH into current branch, try not to form merged commit

    把 BRANCH 合并到以后分支,尽量不造成合并节点

  • git merge --no-ff BRANCH
    Merge BRANCH into current branch, and make sure to form merged commit

    把 BRANCH 合并到以后分支,并确保造成合并节点

  • git merge --squash BRANCH
    Make the differences between BRANCH and the current branch as to-be-committed contents, need to run git commit to complete merging with only one commit

    把 BRANCH 和以后分支的变更作为标识为要提交的内容,须要运行 git commit 实现只有一个提交的合并

git update-index

  • git update-index --assume-unchanged /PATH/TO/FILE
    When a file is modified temporary, but don’t want to be committed, and not suitable to be added to .gitignore, can use this command to make git don’t recognize it as modified
    Cannot use this command if the file is newly added, but can add the file path to .git/info/exclude

    当某个文件被长期批改,但不想提交,也不适宜放到 .gitignore,能够用此命令让 git 不将其辨认为已批改
    如果这个文件是新增的,就不能用这个命令了,不过能够把文件门路加到 .git/info/exclude

  • git update-index --no-assume-unchanged /PATH/TO/FILE
    Recover modified recognition for the designated file

    复原以上文件的批改辨认

正文完
 0