Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
(don’t fear)
the rebase
Ciao
I’m Giorgio Cefaro
Lead Software Engineer
at
@giorrrgio
GIT
“unpleasant, silly,
incompetent, stupid,
annoying, senile, elderly
or childish person.”
https://en.wikipedia.org/wiki/Git_...
1.
git-help
Use it, read it, understand it
Thanks!
Any questions?
@giorrrgio
2.
git-rebase
Put your current branch commits on the top of the
upstream head.
git-rebase
Why not git-merge ?
■ If you merge frequently, lots of merge
commits will create pollution
■ Your commits in yo...
git-rebase under the hood
A git checkout master is executed
If you do not specify an upstream, the configured
upstream wil...
git-rebase under the hood
Current branch is reset to origin/master or, if --onto is
specified, to the specified branch.
Ch...
git-rebase under the hood
If no there are no conflicts during the rebase, you are
done.
Otherwise:
■ resolve the conflicts...
interactive git-rebase
Before applying your local commits, decide what to do
with each of it.
$ git rebase -i master
interactive git-rebase
Interactive rebase of the current branch starting from a
particular hash
$ git rebase -i <hash>^
Live coding!
git push --force
■ use it if you know what you are doing.
■ if you work on the same branch with other people, let
them kno...
forcing a push
■ Refuse to push a branch unless it is the state that we
expect; i.e. nobody has updated the branch upstrea...
“Hey Donald, where
are my commits ?”
git-reflog
■ Reflog is a mechanism to record when the tip of
branches are updated. This command is to manage the
informati...
git-reflog
■ Check what happened in the remote branch
■ Find the right hash before you push forced
$ git reflog show remot...
Live coding!
Bonus git
tips!
3.
Bonus tips!
I bet you’ll find something you didn’t know here :-)
git-add
$ git add -p
■ Add files to the index interactively
■ Review your changes before adding them
y - stage this hunk
n...
interactive git-rebase with autosquash
$ git commit --fixup <hash>
■ Commit changes as a fixup to another commit
■ The com...
interactive git-rebase with autosquash
$ git rebase -i --autosquash
■ Interactive rebase of current branch with automatic
...
git-pull
■ Pull “fast-forward only”: refuse to merge and exit with
a non-zero status unless the current HEAD is already
up...
git-log
■ Format the logs in a more readable way
■ Add some ASCII art to represent branches
■ You can alias it as well for...
git-cherry-pick
■ Cherry pick a serie of consecutive commits to the
current branch
■ Mind the ^: without it you wouldn’t p...
git-stash
■ Stash the uncommitted changes, including untracked
files with a proper name
$ git stash save -u “Ugly fix”
git-stash
■ Take the top of the stash and apply it to a new branch
$ git stash branch new-branch
git-stash
■ Apply a single file from the top of the stash
$ git checkout stash@{0} -- <file_path>
Thanks!
Any questions?
@giorrrgio
Don't fear the rebase
Upcoming SlideShare
Loading in …5
×

Don't fear the rebase

750 views

Published on

A brief overview of the git features I use every day, and some bonus tips

Published in: Software
  • Be the first to comment

Don't fear the rebase

  1. 1. (don’t fear) the rebase
  2. 2. Ciao I’m Giorgio Cefaro Lead Software Engineer at @giorrrgio
  3. 3. GIT
  4. 4. “unpleasant, silly, incompetent, stupid, annoying, senile, elderly or childish person.” https://en.wikipedia.org/wiki/Git_(slang)
  5. 5. 1. git-help Use it, read it, understand it
  6. 6. Thanks! Any questions? @giorrrgio
  7. 7. 2. git-rebase Put your current branch commits on the top of the upstream head.
  8. 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. 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. 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. 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.
  12. 12. interactive git-rebase Before applying your local commits, decide what to do with each of it. $ git rebase -i master
  13. 13. interactive git-rebase Interactive rebase of the current branch starting from a particular hash $ git rebase -i <hash>^
  14. 14. Live coding!
  15. 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. 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 ⚠
  17. 17. “Hey Donald, where are my commits ?”
  18. 18. 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
  19. 19. git-reflog ■ Check what happened in the remote branch ■ Find the right hash before you push forced $ git reflog show remotes/origin/master
  20. 20. Live coding!
  21. 21. Bonus git tips!
  22. 22. 3. Bonus tips! I bet you’ll find something you didn’t know here :-)
  23. 23. 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
  24. 24. 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
  25. 25. 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)
  26. 26. 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
  27. 27. 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
  28. 28. 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>
  29. 29. git-stash ■ Stash the uncommitted changes, including untracked files with a proper name $ git stash save -u “Ugly fix”
  30. 30. git-stash ■ Take the top of the stash and apply it to a new branch $ git stash branch new-branch
  31. 31. git-stash ■ Apply a single file from the top of the stash $ git checkout stash@{0} -- <file_path>
  32. 32. Thanks! Any questions? @giorrrgio

×