(main)$ git show
$ git log -n1 -p
$ git commit --amend --only
$ git commit --amend --only -m 'xxxxxxx'
$ git commit --amend --author "New Authorname "
git filter-branch -f --env-filter '
OLD_EMAIL="原来的邮箱"
CORRECT_NAME="现在的名字"
CORRECT_EMAIL="现在的邮箱"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch Rakefile' HEAD
git push origin --force --all
$ git checkout HEAD^ myfile
$ git add -A
$ git commit --amend
$ git reset HEAD^ --hard
$ git push -f [remote] [branch]
(my-branch*)$ git reset --soft HEAD@{1}
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push -f [remote] [branch]
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
(main)$ git reflog
(main)$ git reset --hard SHA1234
(my-branch*)$ git commit --amend
$ git add --patch filename.x
$ git add -N filename.x
git add
git add -p
$ git commit -m "WIP"
$ git add .
$ git stash
$ git reset HEAD^
$ git stash pop --index 0
$ git checkout -b my-branch
$ git stash
$ git checkout my-branch
$ git stash pop
# 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
(main)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
$ git reset --hard c5bc55a
(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)$ git branch my-branch
(main)$ git reset --hard HEAD^
^
是 HEAD^
1 的简写,可以通过指定要设置的 HEAD 来进一步重置。或者,如果不想使用 HEAD^,找到想重置到的提交(commit)的 hash(git log 能够完成), 然后重置到这个 hash,使用 git push 同步内容到远程。(main)$ git reset --hard a13b85e
HEAD is now at a13b85e
(main)$ git checkout my-branch
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
(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 ..." to unstage)
#
# modified: file1.txt
Note: Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem.
(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
$ 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
(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'
# (--track 是 git checkout -b [branch] [remotename]/[branch] 的简写)
(my-branch)$ git reset --hard ORIG_HEAD
(main)$ git checkout my-branch
(my-branch)$ git rebase -i main
(my-branch)$ git checkout main
(main)$ git merge --ff-only my-branch
(my-branch)$ git reset --soft main
(my-branch)$ git commit -am "New awesome feature"
(my-branch)$ git rebase -i main
~
2 进行 rebase,组合最近 3 次提交(commit),相对于 HEAD~3 等。(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.
(main)$ git merge --no-ff --no-commit my-branch
(main)$ git merge --squash my-branch
(main)$ git rebase -i @{u}
(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
(main)$ git log main ^feature/120-on-scroll --no-merges
noop
(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." 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
$ 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 "commit-message" CREATED_SHA1
$ git clone --recursive git://github.com/foo/bar.git
$ git submodule update --init --recursive
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
$ git fsck --unreachable | grep tag
$ git update-ref refs/tags/<tag_name> <hash>
(main)$ git mv --force myfile MyFile
(main)$ git rm --cached log.txt
[alias]
a = add
amend = commit --amend
c = commit
ca = commit --amend
ci = commit -a
co = checkout
d = diff
dc = diff --changed
ds = diff --staged
f = fetch
loll = log --graph --decorate --pretty=oneline --abbrev-commit
m = merge
one = log --pretty=oneline
outstanding = rebase -i @{u}
s = status
unpushed = log @{u}
wc = whatchanged
wip = rebase -i @{u}
zap = fetch -p
$ git config --global credential.helper cache
# Set git to use the credential memory cache
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
(main)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to main
c10f740 HEAD@{2}: checkout: moving from main to 2.2
$ git reset --hard 0254ea7