我刚才提交了什么?
(main)$ git show
$ git log -n1 -p
我的提交信息(commit message)写错了
$ git commit --amend --only
$ git commit --amend --only -m 'xxxxxxx'
我提交(commit)里的用户名和邮箱不对
$ git commit --amend --author "New Authorname <authoremail@mydomain.com>"
我想从一个提交(commit)里移除一个文件
$ git checkout HEAD^ myfile
$ git add -A
$ git commit --amend
我想删除我的的最后一次提交(commit)
$ git reset HEAD^ --hard
$ git push -f [remote] [branch]
(my-branch*)$ git reset --soft HEAD@{1}
删除任意提交(commit)
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push -f [remote] [branch]
我尝试推一个修正后的提交(amended commit)到远程,但是报错:
To https://github.com/yourusername/repo.git
! [rejected] mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
(my-branch)$ git push origin mybranch -f
我意外的做了一次硬重置(hard reset),我想找回我的内容
(main)$ git reflog
(main)$ git reset --hard SHA1234
暂存(Staging)
我需要把暂存的内容添加到上一次的提交(commit)
(my-branch*)$ git commit --amend
我想要暂存一个新文件的一部分,而不是这个文件的全部
$ git add --patch filename.x
$ git add -N filename.x
我想把在一个文件里的变化(changes)加到两个提交(commit)里
我想把暂存的内容变成未暂存,把未暂存的内容暂存起来
$ git commit -m "WIP"
$ git add .
$ git stash
$ git reset HEAD^
$ git stash pop --index 0
未暂存(Unstaged)的内容
我想把未暂存的内容移动到一个新分支
$ git checkout -b my-branch
我想把未暂存的内容移动到另一个已存在的分支
$ git stash
$ git checkout my-branch
$ git stash pop
我想丢弃本地未提交的变化(uncommitted changes)
# one commit
(my-branch)$ git reset --hard HEAD^
# two commits
(my-branch)$ git reset --hard HEAD^^
# four commits
(my-branch)$ git reset --hard HEAD~4
# or
(main)$ git checkout -f
$ git reset filename
我想丢弃某些未暂存的内容
$ git checkout -p
# Answer y to all of the snippets you want to drop
$ git stash -p
# Select all of the snippets you want to save
$ git reset --hard
$ git stash pop
$ git stash -p
# Select all of the snippets you don't want to save
$ git stash drop
分支(Branches)
我从错误的分支拉取了内容,或把内容拉取到了错误的分支
(main)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
$ git reset --hard c5bc55a
我想扔掉本地的提交(commit),以便我的分支与远程的保持一致
(my-branch)$ git status
# On branch my-branch
# Your branch is ahead of 'origin/my-branch' by 2 commits.
# (use "git push" to publish your local commits)
#
(main)$ git reset --hard origin/my-branch
我需要提交到一个新分支,但错误的提交到了main
(main)$ git branch my-branch
(main)$ git reset --hard HEAD^
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
(main)$ git checkout my-branch
我想保留来自另外一个ref-ish的整个文件
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
-
分支 solution, 拥有原型方案, 领先 develop 分支。 -
分支 develop, 在这里你应用原型方案的一些内容。
(develop)$ git checkout solution -- file1.txt
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1.txt
我把几个提交(commit)提交到了同一个分支,而这些提交应该分布在不同的分支里
(main)$ git log
commit e3851e817c451cc36f2e6f3049db528415e3c114
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:27 2014 -0400
Bug #21 - Added CSRF protection
commit 5ea51731d150f7ddc4a365437931cd8be3bf3131
Author: Alex Lee <alexlee@example.com>
Date: Tue Jul 22 15:39:12 2014 -0400
Bug #14 - Fixed spacing on title
commit a13b85e984171c6e2a1729bb061994525f626d14
Author: Aki Rose <akirose@example.com>
Date: Tue Jul 21 01:12:48 2014 -0400
First commit
(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
(main)$ git checkout -b 21
(21)$
(21)$ git cherry-pick e3851e8
(21)$ git checkout main
(main)$ git checkout -b 14
(14)$
(14)$ git cherry-pick 5ea5173
我想删除上游(upstream)分支被删除了的本地分支
$ git fetch -p
我不小心删除了我的分支
(main)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt
(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(my-branch)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <siemiatj@example.com>
Date: Wed Jul 30 00:34:10 2014 +0200
foo.txt added
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <katehudson@example.com>
Date: Tue Jul 29 13:14:46 2014 -0400
Fixes #6: Force pushing after amending commits
(my-branch)$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
(main)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(main)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
(main)$ git reflog
69204cd HEAD@{0}: checkout: moving from my-branch to main
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from main to my-branch
(main)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt
我想删除一个分支
(main)$ git push origin --delete my-branch
(main)$ git push origin :my-branch
(main)$ git branch -D my-branch
我想从别人正在工作的远程分支签出(checkout)一个分支
(main)$ git fetch --all
(main)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin.
Switched to a new branch 'daves'
Rebasing 和合并(Merging)
我想撤销rebase/merge
(my-branch)$ git reset --hard ORIG_HEAD
我已经rebase过, 但是我不想强推(force push)
(main)$ git checkout my-branch
(my-branch)$ git rebase -i main
(my-branch)$ git checkout main
(main)$ git merge --ff-only my-branch
我需要组合(combine)几个提交(commit)
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"
(my-branch)$ git rebase -i main
(main)$ git rebase -i HEAD~2
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix
# Rebase 8074d12..b729ad5 onto 8074d12
#
# 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
s b729ad5 fixup
s e3851e8 another fix
Newer, awesomer features
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 8074d12
# You are currently editing a commit while rebasing branch 'main' on '8074d12'.
#
# Changes to be committed:
# modified: README.md
#
(main)$ Successfully rebased and updated refs/heads/main.
安全合并(merging)策略
(main)$ git merge --no-ff --no-commit my-branch
我需要将一个分支合并成一个提交(commit)
(main)$ git merge --squash my-branch
我只想组合(combine)未推的提交(unpushed commit)
(main)$ git rebase -i @{u}
检查是否分支上的所有提交(commit)都合并(merge)过了
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
(main)$ git log main ^feature/120-on-scroll --no-merges
交互式rebase(interactive rebase)可能出现的问题
这个rebase 编辑屏幕出现'noop'
noop
-
检查确保主(main)分支没有问题 -
rebase HEAD~2 或者更早
有冲突的情况
(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
<<<<<<< HEAD
some code
=========
some code
>>>>>>> new-commit
(main*)$ git mergetool -t opendiff
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
(my-branch)$ git rebase --abort
Stash
暂存所有改动
$ git stash
$ git stash -u
暂存指定文件
$ git stash push working-directory-path/filename.ext
$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext
暂存时记录消息
$ git stash save <message>
$ git stash push -m <message>
使用某个指定暂存
$ git stash list
$ git stash apply "stash@{n}"
$ git stash apply "stash@{2.hours.ago}"
暂存时保留未暂存的内容
$ git stash create
$ git stash store -m
文章来源于: 单片机与嵌入式
原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。