前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Git】Common Git Command Line Operation

【Git】Common Git Command Line Operation

作者头像
阿东
发布2024-03-05 15:03:47
1220
发布2024-03-05 15:03:47
举报

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 addgit 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/p, edit/e, drop/d, squash/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 恢复以上文件的修改识别
本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-03-01,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 懒时小窝 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Source
    • git clone
      • git fetch
        • git pull
          • git push
            • git branch
              • git checkout
                • git add
                  • git commit
                    • git cherry-pick
                      • git status
                        • git diff
                          • git log
                            • git stash
                              • git revert
                                • git reset
                                  • git rebase
                                    • git merge
                                      • git update-index
                                      领券
                                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                                      http://www.vxiaotou.com