赞
踩
承接上一篇 《git 入门篇》,详细讲解 git 命令具体应用。
提示:git 作为基础工具,代码开发 迭代维护非常适用。
① 查看项目的分支
git branch #本地
git branch -r #远程
git branch -a #本地和远程
② 切换分支
git swtich branch_name
git checkout -b branch_name #切换到新分支
③ 重命名git本地分支
git branch -m old_local_branch_name new_local_branch_name
④ 重命名远程分支对应的本地分支(先删除再推送新名字分支)
git push origin_name --delete old_local_branch_name #删除远程分支
git branch -m old_local_branch_name new_local_branch_name #重命名本地分支
git push origin_name new_local_branch_name #提交新分支
⑤ 删除本地分支
git branch -d <BranchName>
⑥ 删除远程分支
git push origin <BranchName> --delete
⑦ 推送本地分支
#默认推送到与本地分支名相同远程分支
git push origin local_branch
#本地推送到指定远程分支
git push origin local_branch:remote_branch
⑧ 提交覆盖更新远程分支
#强制更新分支,当前分支提交
git push origin branch --force
# 强制更新到指定提交点
git push origin HEAD --force
⑨ 删除本地跟踪 remote
git branch -rd origin/branch
① 查看某个问题修改记录
a)可列出文件的所有改动历史
git log --pretty=oneline path/fileName
b) 与文件名相关的commit记录
git log path/fileName
c) 只看某次提交中的某个文件变化,可以直接加上fileName
git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b path
git show c5e69804bbd9725b5dece57f8cbece4a96b9f80b path/fileName
② 本地代码回退
a)强制回退到某个版本 不保留修改项
git reset commit-id --hard
b)代码软回退 保留修改项
git reset commit-id
git reset commit-id --soft
③ 代码回滚
a) 回滚单次提交 并生成提交记录
git revert commit-id
b) 回滚多个提交
git revert -n commit-id-old commit-id-new
git commit ./ -m "brief"
或者变基更新,生成新 commit
git rebase -i commit-id
c) 代码回滚但不要提交记录
① 回退到指定commit
git reset --hard <commit-id>
② 强制覆盖提交
git push remote branch --force
git push remote local_branch:remote_branch --force
```
## 1.3 git 添加 / 删除文件
1) 添加文件
git add -A 所有变化文件
-u 添加被修改(modified)和被删除(deleted)文件,不包括新文件(new)
. 添加新文件(new)和被修改(modified)文件,不包括被删除(deleted)文件
添加新文件
```bash
git add $(git status -u <path>) #验证无效
git add $(git status -s <path> | awk -F ' ' '{ print $2 }')
# 删除追踪文件
git rm $(git ls-files --deleted)
# 删除指定目录下的全部文件
git rm $(git ls-files <path> --deleted)
# 删除未被追踪文件
git clean -df
git ls-files -d | xargs git rm
git ls-files --others --exclude-standard
添加未追踪文件
git sstatus --porcelain | grep '^??' | cut -c4- | xargs git add
#或者
git ls-files -z -o --exclude-standard | xargs -0 git add
a) 提交代码到本地
git commit path/file -m "commit log"
推送到远程
git push remote_name local_branch:remoteb_branch
b) 拉取代码到本地
git fetch remote_name
git pull remote_name local_branch:remoteb_branch
a) 查看过往被删提交记录
git reflog
6891e36 HEAD@{0}: reset: moving to 6891e3609ce9dff84ed1233bb21e33b235823684
ddfe728 HEAD@{1}: checkout: moving from hra to t6s
b) 恢复指定删除提交
git cherry-pick 6891e36
a) 显示本地tag
git tag
b) tag 增加
git tag tag_name
git psuh remote_name branch_name tag_name
c) 删除远程tag
Step1: 删除本地tag
git tag -d tagName
Step2: 删除远程tag
git push origin branch :refs/tags/tagName
Tips
git异常锁库
Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
$ rm .git/index.lock #简单粗暴处理
查看当前分支,讲解分支合并两种常用方式 rebase
和 merge
,先来讲 git merge
git branch
* 356x-harmony
356x-online
master
# master 分支已经同步到最新代码,当前 356x-harmony 希望合并更新
git merge master
详细的参数可以通过 --help
参数查看
git merge --help NAME git-merge - Join two or more development histories together SYNOPSIS git merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit] [--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]] [--[no-]allow-unrelated-histories] [--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>...] git merge (--continue | --abort | --quit) DESCRIPTION Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. This command is used by git pull to incorporate changes from another repository and can be used by hand to merge changes from one branch into another. Assume the following history exists and the current branch is "master": A---B---C topic / D---E---F---G master Then "git merge topic" will replay the changes made on the topic branch since it diverged from master (i.e., E) until its current commit (C) on top of master, and record the result in a new commit along with the names of the two parent commits and a log message from the user describing the changes. A---B---C topic / \ D---E---F---G---H master The second syntax ("git merge --abort") can only be run after the merge has resulted in conflicts. git merge --abort will abort the merge process and try to reconstruct the pre-merge state. However, if there were uncommitted changes when the merge started (and especially if those changes were further modified after the merge was started), git merge --abort will in some cases be unable to reconstruct the original (pre-merge) changes. Therefore: Warning: Running git merge with non-trivial uncommitted changes is discouraged: while possible, it may leave you in a state that is hard to back out of in the case of a conflict. The third syntax ("git merge --continue") can only be run after the merge has resulted in conflicts.
换乘 rebase
变基更新
git rebase -i master
详细的参数可以通过 --help
参数查看
NAME git-rebase - Reapply commits on top of another base tip SYNOPSIS git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase> | --keep-base] [<upstream> [<branch>]] git rebase [-i | --interactive] [<options>] [--exec <cmd>] [--onto <newbase>] --root [<branch>] git rebase (--continue | --skip | --abort | --quit | --edit-todo | --show-current-patch) DESCRIPTION If <branch> is specified, git rebase will perform an automatic git switch <branch> before doing anything else. Otherwise it remains on the current branch. If <upstream> is not specified, the upstream configured in branch.<name>.remote and branch.<name>.merge options will be used (see git-config(1) for details) and the --fork-point option is assumed. If you are currently not on any branch or if the current branch does not have a configured upstream, the rebase will abort. All changes made by commits in the current branch but that are not in <upstream> are saved to a temporary area. This is the same set of commits that would be shown by git log <upstream>..HEAD; or by git log 'fork_point'..HEAD, if --fork-point is active (see the description on --fork-point below); or by git log HEAD, if the --root option is specified. The current branch is reset to <upstream>, or <newbase> if the --onto option was supplied. This has the exact same effect as git reset --hard <upstream> (or <newbase>). ORIG_HEAD is set to point at the tip of the branch before the reset. The commits that were previously saved into the temporary area are then reapplied to the current branch, one by one, in order. Note that any commits in HEAD which introduce the same textual changes as a commit in HEAD..<upstream> are omitted (i.e., a patch already accepted upstream with a different commit message or timestamp will be skipped). It is possible that a merge failure will prevent this process from being completely automatic. You will have to resolve any such merge failure and run git rebase --continue. Another option is to bypass the commit that caused the merge failure with git rebase --skip. To check out the original <branch> and remove the .git/rebase-apply working files, use the command git rebase --abort instead. Assume the following history exists and the current branch is "topic": A---B---C topic / D---E---F---G master From this point, the result of either of the following commands: git rebase master git rebase master topic would be: A'--B'--C' topic / D---E---F---G master NOTE: The latter form is just a short-hand of git checkout topic followed by git rebase master. When rebase exits topic will remain the checked-out branch.
git clone https://github.com/chewitt/RTL8822CS.git
cd RTL8822CS
# 查看远程仓情况
git remote -v
origin https://github.com/chewitt/RTL8822CS.git (fetch)
origin https://github.com/chewitt/RTL8822CS.git (push)
或者进入到本地代码仓库,
cd harmony/kernel/linux/linux-5.10/
git remote -v
origin https://gitee.com/openharmony/kernel_linux_5.10 (fetch)
origin https://gitee.com/openharmony/kernel_linux_5.10 (push)
- 增加远程仓
git 管理过程难免会远程仓转换,用到命令 git remote
建立链接,提交代码即可
命令中 remote_name
远程仓名, remote-url
则是 git 地址
git remote add remote_name remote-url
#如果想提交到远程代码托管
git push remote_name branch
远程仓创建完成,可以再使用 git fetch remote_name
和 git pull remote_namel
完成代码同步。
如果想重新拉取可以尝试如下操作
git pull --rebase origin master
- 删除远程仓
git remote prune remote_name
#或者
git remote remove remote_name
详细的参数可以通过 --help
参数查看
git remote --help NAME git-remote - Manage set of tracked repositories SYNOPSIS git remote [-v | --verbose] git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url> git remote rename <old> <new> git remote remove <name> git remote set-head <name> (-a | --auto | -d | --delete | <branch>) git remote set-branches [--add] <name> <branch>... git remote get-url [--push] [--all] <name> git remote set-url [--push] <name> <newurl> [<oldurl>] git remote set-url --add [--push] <name> <newurl> git remote set-url --delete [--push] <name> <url> git remote [-v | --verbose] show [-n] <name>... git remote prune [-n | --dry-run] <name>... git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...] DESCRIPTION Manage the set of repositories ("remotes") whose branches you track. OPTIONS -v, --verbose Be a little more verbose and show remote url after name. NOTE: This must be placed between remote and subcommand.
#生成补丁
git format-patch -n #默认以最新提交为基础, 向后 n 个补丁
git format-patch --HEAD -n # HEAD 提交记录 hash 值为基的向后 n 个补丁
diff 则比较随意
git diff path > xxxx.diff # path 默认为当前目录
-应用打补丁(patch),或临时修改(diff)
打补丁前建先检查再应用,如果检查通过则可以直接应用
git apply --check 00xx-xxxxx.patch
#应用补丁,但不提交
git apply 00xx-xxxxx.patch
#应用补丁和提交记录
git am 00xx-xxxxx.patch
应用临时修改 diff文件
patch -p 01 < xxxx.diff
Tips
如果想要补丁不能直接使用,该如何处理?
答:使用patch 命令直接打补丁,然后再核对修改点。
参照 1.5 中 git reflog
查看删除的提交记录, 找到 MD5值;根据 MD5 值抽取指定提交点,git cherry-pick MD5
恢复已经删除的提交记录
Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
简单粗暴解决方法 删掉 index.lock
rm .git/index.lock
a) 克隆新代码仓,结束后无任何文件并有错误提示。
warning: 远程 HEAD 指向一个不存在的引用,无法检出。
未构建远程 master 分支,检出文件时定位不到代码主分支;解决方法:构建 master 分支,并上传到远程仓。
b) 查看从<commit_id1>
到<commit_id2>
之间的所有提交记录
git log <commit_id1>..<commit_id2>
c) 查看某个commit之前的所有提交记录
git log <commit_id>..
d) 查看某个commit之后的所有提交记录
git log ..<commit_id>
好工具提升工作效率。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。