Git Habits

that made me
more aware of what I'm doing

Summary

  • Command line only, no IDEs
  • Unrelated to git workflows (gitflow, etc.) 

Git-related "best practices" I use for efficiency and exercise

Restrictions

VERY IMPORTANT

  • Branch history is made of commits
  • Commits have a parent (like a "tree")
  • branches and tags are POINTERS to their latest commit

Delete merged branches often

git branch --merged | grep -v "*"|awk '{print "git branch -d " $1}'|bash

Fetch often

git fetch --prune
  • first thing you do when starting work on a project
  • remote references might not be updated
  • no pull. We can merge or rebase later

Why not "pull"?

git pull

Status often

git status
  • which branch are you on?
  • what is staged?
    • And unstaged?
    • And not even included?
  • which branch (if any), is this keeping track of?

Need to force push? Add a lease!

git push <remote_probably_origin> <branch> --force-with-lease
  • prevent losing changes from other users
  • used to update a remote branch with undesired commits (like WIPs)

The oh-whoops!
commit

git commit ...
# forgot to add something, or wants to replace commit message
git commit ... --amend
  • "fixes" commits with mistakes
  • actually replaces commit with a new one (if files are involved)
  • won't work if already pushed (unless you force push)

Need to force push? Add a lease!

git push <remote_probably_origin> <branch> --force-with-lease
  • prevent losing changes from other users
  • used to update a remote branch with undesired commits (like WIPs)

Not sure what to include? Try -p

git add -p ...
git checkout -p ...
git reset -p ...
git log -p ...
  • review your work
  • add, remove or reset "hunks" (editable parts) of your changes
  • does NOT include untracked files

Reorganize AND Sync your work with remote target branch

git rebase -i <remote_branch>
# example: git rebase -i origin/develop
  • add your local commits to the tip of the remote branch -- it's rebase, after all...
  • reorganize your new commits
  • ideal right before a PR (and after git fetch)

Get file from another commit (or branch, or tag...)

git checkout <branch_or_tag_or_commit> -- <path_to_file>
  • useful to "recover" files (even deleted ones)
  • can be used with -p for review
  • -- is optional, I think
  • git restore?

Recall what you did

git reflog

Pick a commit from somewhere else

git cherry-pick <some-commit-or-a-pointer-to-a-commit>
  • creates a NEW commit (with same changes) to adapt changes to the current branch
  • referring to a branch or tag picks the LATEST commit
    • remember: branches and tags are POINTERS to their latest commit

Log often

Subtitle

Logs that give you an overall scope of current events

git log <branch> --graph
git log <branch> --oneline

Log since a specific time

git log --since="2 weeks ago"
git log --since="1 month ago"
git log --since="2 years ago"

see https://git-scm.com/docs/git-log

Logs for specific files

git log -- <file-path>

see https://git-scm.com/docs/git-log

A better Git Blame

by Andrew Ray (see blog post)

 

 

What's wrong with
Git Blame?

Git blame detects the commit with the last change in a line (could be indentation, refactoring, etc.)

 

Might not be what you want ​
(to find out where the bug came from) 

Solution is...
git log?

Then, search for the intended change:

(in Vim) /something ("Enter" and 'n' or 'N' to navigate)

git log -p -m --follow --stat -- path/to/your/file  

Rebase to sync your local branches with their target (usually 'dev')

git rebase <remote-branch>
# ex.: git rebase origin/develop

Not going well?
git rebase --abort

Those new git commands

git switch <branch>
# same as git checkout <branch>

git restore
# git reset 
# ex: git restore .
# ex.: git restore --staged <file-path>
# ex.: git restore --source <some-commit-hash> <file-path>

Less common habits

More like "tricks"

Find default remote branch

git remote show <remote_name>
# ex.: git remote show origin
# check HEAD branch field

and what's being tracked

Only the name of the default branch?

git remote show <remote_name> | grep 'HEAD branch' | cut -d' ' -f5

References

Git Stories

By Miguel Costa

Git Stories

In this presentation, I'd like to share with you some interesting stuff about system versioning with GIT.

  • 384