The document provides tips and explanations for various Git commands. It discusses git-rebase and how it works under the hood by resetting the current branch to the upstream head and reapplying commits on top. It also discusses interactive rebasing, forcing a push, using git-reflog to view past branch updates, and bonus tips for commands like git-add, git-pull, git-log, git-cherry-pick, and git-stash.
8. git-rebase
Why not git-merge ?
¡ö If you merge frequently, lots of merge
commits will create pollution
¡ö Your commits in your current branch will
go down in the history
9. git-rebase under the hood
A git checkout master is executed
If you do not specify an upstream, the configured
upstream will be used. If no branch available in upstream,
rebase will abort.
Current changes are saved in a temporary area.
$ git rebase master
10. git-rebase under the hood
Current branch is reset to origin/master or, if --onto is
specified, to the specified branch.
Changes saved before are now applied on the top of the
new base, commit by commit.
$ git rebase master
Note: any commits with same textual changes already introduced in the upstream branch are
skipped (most of the times you can safely try to cherry pick something from another branch and
then let git remove the duplicates patches on a future rebase)
11. git-rebase under the hood
If no there are no conflicts during the rebase, you are
done.
Otherwise:
¡ö resolve the conflicts and git rebase --continue
¡ö skip the offending patch with git rebase --skip
¡ö abort the rebase with git rebase --abort
$ git rebase master
Note: aborting during a rebase is perfectly safe, you will go back to the exact point you were before
the rebase.
15. git push --force
¡ö use it if you know what you are doing.
¡ö if you work on the same branch with other people, let
them know before.
¡ö before a push -f be sure that nobody has pushed
anything on the same branch before you and you will
probably be safe to do it.
¡ö It's hard that something is unrecoverable.
$ git push --force ?
16. forcing a push
¡ö Refuse to push a branch unless it is the state that we
expect; i.e. nobody has updated the branch upstream.
¡ö How? checking that the upstream ref is what we
expect.
¡ö does not work if the last time I updated with a git fetch
(the working tree is not altered)
$ git push --force-with-lease ?
19. git-reflog
¡ö Reflog is a mechanism to record when the tip of
branches are updated. This command is to manage the
information recorded in it.
¡ö You can take a look of what happened back in time
with a fine grain!
¡ö If something went wrong, just find the right hashbefore
the disaster and reset --hard <hash>
$ git reflog
20. git-reflog
¡ö Check what happened in the remote branch
¡ö Find the right hash before you push forced
$ git reflog show remotes/origin/master
24. git-add
$ git add -p
¡ö Add files to the index interactively
¡ö Review your changes before adding them
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
25. interactive git-rebase with autosquash
$ git commit --fixup <hash>
¡ö Commit changes as a fixup to another commit
¡ö The commit message will start with ¡°fixup! ¡±
¡ö Very useful for code reviews
26. interactive git-rebase with autosquash
$ git rebase -i --autosquash
¡ö Interactive rebase of current branch with automatic
fixup commits positioning (commits noted with a
¡°fixup!¡± prefix will be repositioned after their fixupped
commits)
27. git-pull
¡ö Pull ¡°fast-forward only¡±: refuse to merge and exit with
a non-zero status unless the current HEAD is already
up-to-date or the merge can be resolved as a
fast-forward.
¡ö Alias it as git ff:
git config --global alias.ff pull --ff-only
$ git pull --ff-only
28. git-log
¡ö Format the logs in a more readable way
¡ö Add some ASCII art to represent branches
¡ö You can alias it as well for convenience
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s
%Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
29. git-cherry-pick
¡ö Cherry pick a serie of consecutive commits to the
current branch
¡ö Mind the ^: without it you wouldn¡¯t pick the first
commit
$ git cherry-pick <from_hash>^..<to_hash>
30. git-stash
¡ö Stash the uncommitted changes, including untracked
files with a proper name
$ git stash save -u ¡°Ugly fix¡±
31. git-stash
¡ö Take the top of the stash and apply it to a new branch
$ git stash branch new-branch
32. git-stash
¡ö Apply a single file from the top of the stash
$ git checkout stash@{0} -- <file_path>