开发常用命令合集 Git 篇

Git(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 也是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件。分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库。

基本使用

# 克隆仓库
git clone git@github.com:GerritCodeReview/gerrit.git

# 查看分支
git branch
git branch -a
git branch -vva

# 切换分支
git checkout main
git checkout -b main origin/main

# 查看修改状态
git status .

# 查看修改内容
git diff .

# 提交仓库方法
git add .
git commit -m "commit msg"
git push origin main

# 查看提交记录
git log
git log --oneline
git log --oneline --grep="" --after=""  --before="" --author="" .
git reflog

# 查看修改详情
git show .
git show commit_id
git show commit_id --stat

# 初始化仓库
git init

# 拉取更新
git pull
git pull --rebase

# 清理修改
git clean 
git clean -xdf
git clean -df

# 将没有放入缓存区(git add)之前的文件恢复到工作区状态
git checkout .

# 回退版本
git reset --hard HEAD^
git reset --hard HEAD~5 
git reset --hard commit_id
git reset --hard FETCH_HEAD

# 将commit的最后一笔回退到暂存区
# commit-id 为要回退的这笔提交的前一个commitId
git reset commit-id 

# 暂存
git stash
# 暂存取出
git stash pop

Git tag 用法

tag是git版本库的一个标记,指向某个commit的指针。

# 创建本地tag(基于本地当前分支的最后的一个commit 创建的 tag)
git tag <tagName> 
# 推送到远程仓库
git push origin <tagName> 
# 若存在很多未推送的本地标签,你想一次全部推送的话:
git push origin --tags
# 只想以某一个特定的提交为tag ,只要你知道commit 的id。
git tag -a <tagName> <commitId>
# 可以指定tag标签信息。
git tag -a <tagname> -m "XXX..." 

# 查看本地某个 tag 的详细信息:
git show <tagName>
# 查看本地所有 tag:
git tag 或者 git tag -l
# 查看远程所有 tag:
git ls-remote --tags origin

# 本地 tag 的删除:
git tag -d <tagName>
# 远程 tag 的删除:
git push origin :refs/tags/<tagName>

# 检出标签
git checkout -b <branchName> <tagName>

Git pick 用法

1.选择需要pick的节点,前不含后含,pick命令:
git cherry-pick 8b3401f8b...11a5383ae

2.后续根据提示解决冲突,add 修改后,使用命令:
git cherry-pick --continue ,直到没有冲突提示即可

3.提交:git push origin HEAD:refs/for/freeme-11.0.0_master

4.如提交报错

To ssh://10.20.40.21:29418/Freeme/platforms/common/apps/FreemeSettings
! [remote rejected]     HEAD -> refs/for/freeme-11.0.0_master (change http://10.20.40.21:8081/#/c/Freeme/platforms/common/apps/FreemeSettings/+/99765 closed)
error: failed to push some refs to 'ssh://fangjian@10.20.40.21:29418/Freeme/platforms/common/apps/FreemeSettings'

1)根据提示链接中的commit message找到此提交,使用命令git rebase -i 删除提交节点即可

2)如链接无法在gerrit中打开,使用二分法提交,直到找到这笔提交,可以使用该命令进行部分提交:

git push origin da75bc971:refs/for/freeme-11.0.0_master

Git merge用法

prod分支 -- 产品分支
master分支 -- 平时修改分支

同步代码:
先切换到要同步到的分支上(prod分支 -- product分支),remotes/origin后面跟平时修改的分支
git merge --no-ff --no-commit remotes/origin/freeme-11.0.0_master-sprd
# merge 的时候注意冲突修复
# commit的是从哪里merge的
git commit -m "ROUTINE: merge branch 'freeme-11.0.0_master-sprd'"
git push origin HEAD:refs/for/xxxx

Git 冲突解决

git合并时冲突 解决方法:
<<<<<<< HEAD
本地代码
=======
拉下来的代码
>>>>>>>
根据需要删除代码就行了,完事把<<<<<<<    =======     >>>>>>都删掉冲突就解决了

Git lg 配置

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

AOSP输出全部修改点

repo forall -pc 'git lg --after="2022-11-8" --before="2022-11-24"' > commit.txt

Gerrit pick 用法

1.Gerrit - 某个提交点 - Download - Cherry Pick - copy pick命令
2.在本地仓库运行此命令
3.正常运行即成功,即可提交到远程仓库git push origin HEAD:refs/for/main

如报错
1.解决冲突
2.git add <modify-file-path>
3.git cherry-pick  --continue
4.git push origin HEAD:refs/for/main

Git rebase 用法

git rebase的两种用法(最全)_小垚尧的博客-CSDN博客_git rebase

# 变基操作
git rebase -i

# 合并多个commit,从上到下,commit 越新
在 commit 前修改 pick 为 s 后保存即可

# 修改commit记录
注释多余提交信息后保存即可

pick	p	保留该commit
reword	r	保留该commit,但需要修改该commit的注释
edit	e	保留该commit, 但我要停下来修改该提交(不仅仅修改注释)
squash	s	将该commit合并到前一个commit
fixup	f	将该commit合并到前一个commit,但不要保留该提交的注释信息
exec	x	执行shell命令
drop	d	丢弃该commit

全部命令

$ git help -a
See 'git help <command>' to read about a specific subcommand

Main Porcelain Commands
   add                  Add file contents to the index
   am                   Apply a series of patches from a mailbox
   archive              Create an archive of files from a named tree
   bisect               Use binary search to find the commit that introduced a bug
   branch               List, create, or delete branches
   bundle               Move objects and refs by archive
   checkout             Switch branches or restore working tree files
   cherry-pick          Apply the changes introduced by some existing commits
   citool               Graphical alternative to git-commit
   clean                Remove untracked files from the working tree
   clone                Clone a repository into a new directory
   commit               Record changes to the repository
   describe             Give an object a human readable name based on an available ref
   diff                 Show changes between commits, commit and working tree, etc
   fetch                Download objects and refs from another repository
   format-patch         Prepare patches for e-mail submission
   gc                   Cleanup unnecessary files and optimize the local repository
   gitk                 The Git repository browser
   grep                 Print lines matching a pattern
   gui                  A portable graphical interface to Git
   init                 Create an empty Git repository or reinitialize an existing one
   log                  Show commit logs
   merge                Join two or more development histories together
   mv                   Move or rename a file, a directory, or a symlink
   notes                Add or inspect object notes
   pull                 Fetch from and integrate with another repository or a local branch
   push                 Update remote refs along with associated objects
   range-diff           Compare two commit ranges (e.g. two versions of a branch)
   rebase               Reapply commits on top of another base tip
   reset                Reset current HEAD to the specified state
   restore              Restore working tree files
   revert               Revert some existing commits
   rm                   Remove files from the working tree and from the index
   shortlog             Summarize 'git log' output
   show                 Show various types of objects
   sparse-checkout      Initialize and modify the sparse-checkout
   stash                Stash the changes in a dirty working directory away
   status               Show the working tree status
   submodule            Initialize, update or inspect submodules
   switch               Switch branches
   tag                  Create, list, delete or verify a tag object signed with GPG
   worktree             Manage multiple working trees

Ancillary Commands / Manipulators
   config               Get and set repository or global options
   fast-export          Git data exporter
   fast-import          Backend for fast Git data importers
   filter-branch        Rewrite branches
   mergetool            Run merge conflict resolution tools to resolve merge conflicts
   pack-refs            Pack heads and tags for efficient repository access
   prune                Prune all unreachable objects from the object database
   reflog               Manage reflog information
   remote               Manage set of tracked repositories
   repack               Pack unpacked objects in a repository
   replace              Create, list, delete refs to replace objects

Ancillary Commands / Interrogators
   annotate             Annotate file lines with commit information
   blame                Show what revision and author last modified each line of a file
   bugreport            Collect information for user to file a bug report
   count-objects        Count unpacked number of objects and their disk consumption
   difftool             Show changes using common diff tools
   fsck                 Verifies the connectivity and validity of the objects in the database
   gitweb               Git web interface (web frontend to Git repositories)
   help                 Display help information about Git
   instaweb             Instantly browse your working repository in gitweb
   merge-tree           Show three-way merge without touching index
   rerere               Reuse recorded resolution of conflicted merges
   show-branch          Show branches and their commits
   verify-commit        Check the GPG signature of commits
   verify-tag           Check the GPG signature of tags
   whatchanged          Show logs with difference each commit introduces

Interacting with Others
   archimport           Import a GNU Arch repository into Git
   cvsexportcommit      Export a single commit to a CVS checkout
   cvsimport            Salvage your data out of another SCM people love to hate
   cvsserver            A CVS server emulator for Git
   imap-send            Send a collection of patches from stdin to an IMAP folder
   p4                   Import from and submit to Perforce repositories
   quiltimport          Applies a quilt patchset onto the current branch
   request-pull         Generates a summary of pending changes
   send-email           Send a collection of patches as emails
   svn                  Bidirectional operation between a Subversion repository and Git

Low-level Commands / Manipulators
   apply                Apply a patch to files and/or to the index
   checkout-index       Copy files from the index to the working tree
   commit-graph         Write and verify Git commit-graph files
   commit-tree          Create a new commit object
   hash-object          Compute object ID and optionally creates a blob from a file
   index-pack           Build pack index file for an existing packed archive
   merge-file           Run a three-way file merge
   merge-index          Run a merge for files needing merging
   mktag                Creates a tag object
   mktree               Build a tree-object from ls-tree formatted text
   multi-pack-index     Write and verify multi-pack-indexes
   pack-objects         Create a packed archive of objects
   prune-packed         Remove extra objects that are already in pack files
   read-tree            Reads tree information into the index
   symbolic-ref         Read, modify and delete symbolic refs
   unpack-objects       Unpack objects from a packed archive
   update-index         Register file contents in the working tree to the index
   update-ref           Update the object name stored in a ref safely
   write-tree           Create a tree object from the current index

Low-level Commands / Interrogators
   cat-file             Provide content or type and size information for repository objects
   cherry               Find commits yet to be applied to upstream
   diff-files           Compares files in the working tree and the index
   diff-index           Compare a tree to the working tree or index
   diff-tree            Compares the content and mode of blobs found via two tree objects
   for-each-ref         Output information on each ref
   get-tar-commit-id    Extract commit ID from an archive created using git-archive
   ls-files             Show information about files in the index and the working tree
   ls-remote            List references in a remote repository
   ls-tree              List the contents of a tree object
   merge-base           Find as good common ancestors as possible for a merge
   name-rev             Find symbolic names for given revs
   pack-redundant       Find redundant pack files
   rev-list             Lists commit objects in reverse chronological order
   rev-parse            Pick out and massage parameters
   show-index           Show packed archive index
   show-ref             List references in a local repository
   unpack-file          Creates a temporary file with a blob's contents
   var                  Show a Git logical variable
   verify-pack          Validate packed Git archive files

Low-level Commands / Syncing Repositories
   daemon               A really simple server for Git repositories
   fetch-pack           Receive missing objects from another repository
   http-backend         Server side implementation of Git over HTTP
   send-pack            Push objects over Git protocol to another repository
   update-server-info   Update auxiliary info file to help dumb servers

Low-level Commands / Internal Helpers
   check-attr           Display gitattributes information
   check-ignore         Debug gitignore / exclude files
   check-mailmap        Show canonical names and email addresses of contacts
   check-ref-format     Ensures that a reference name is well formed
   column               Display data in columns
   credential           Retrieve and store user credentials
   credential-store     Helper to store credentials on disk
   fmt-merge-msg        Produce a merge commit message
   interpret-trailers   Add or parse structured information in commit messages
   mailinfo             Extracts patch and authorship from a single e-mail message
   mailsplit            Simple UNIX mbox splitter program
   merge-one-file       The standard helper program to use with git-merge-index
   patch-id             Compute unique ID for a patch
   sh-i18n              Git's i18n setup code for shell scripts
   sh-setup             Common Git shell script setup code
   stripspace           Remove unnecessary whitespace

External commands
   askyesno
   credential-helper-selector
   flow
   lfs
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值