33
(don’t fear) the rebase

Don't fear the rebase

Embed Size (px)

Citation preview

Page 1: Don't fear the rebase

(don’t fear)the rebase

Page 2: Don't fear the rebase

CiaoI’m Giorgio Cefaro

Lead Software Engineerat

@giorrrgio

Page 3: Don't fear the rebase

GIT

Page 4: Don't fear the rebase

“unpleasant, silly, incompetent, stupid,

annoying, senile, elderly or childish person.”

https://en.wikipedia.org/wiki/Git_(slang)

Page 5: Don't fear the rebase

1.git-help

Use it, read it, understand it

Page 6: Don't fear the rebase

Thanks!Any questions?

@giorrrgio

Page 7: Don't fear the rebase

2.git-rebase

Put your current branch commits on the top of the upstream head.

Page 8: Don't fear the rebase

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

Page 9: Don't fear the rebase

git-rebase under the hood

A git checkout master is executedIf 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

Page 10: Don't fear the rebase

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)

Page 11: Don't fear the rebase

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.

Page 12: Don't fear the rebase

interactive git-rebase

Before applying your local commits, decide what to do with each of it.

$ git rebase -i master

Page 13: Don't fear the rebase

interactive git-rebase

Interactive rebase of the current branch starting from a particular hash

$ git rebase -i <hash>^

Page 14: Don't fear the rebase

Live coding!

Page 15: Don't fear the rebase

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 ⚠

Page 16: Don't fear the rebase

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 ⚠

Page 17: Don't fear the rebase

“Hey Donald, where are my commits ?”

Page 18: Don't fear the rebase
Page 19: Don't fear the rebase

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

Page 20: Don't fear the rebase

git-reflog

■ Check what happened in the remote branch■ Find the right hash before you push forced

$ git reflog show remotes/origin/master

Page 21: Don't fear the rebase

Live coding!

Page 22: Don't fear the rebase

Bonus git tips!

Page 23: Don't fear the rebase

3.Bonus tips!

I bet you’ll find something you didn’t know here :-)

Page 24: Don't fear the rebase

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

Page 25: Don't fear the rebase

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

Page 26: Don't fear the rebase

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)

Page 27: Don't fear the rebase

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

Page 28: Don't fear the rebase

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

Page 29: Don't fear the rebase

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>

Page 30: Don't fear the rebase

git-stash

■ Stash the uncommitted changes, including untracked files with a proper name

$ git stash save -u “Ugly fix”

Page 31: Don't fear the rebase

git-stash

■ Take the top of the stash and apply it to a new branch

$ git stash branch new-branch

Page 32: Don't fear the rebase

git-stash

■ Apply a single file from the top of the stash

$ git checkout stash@{0} -- <file_path>

Page 33: Don't fear the rebase

Thanks!Any questions?

@giorrrgio