Different commands and options to make your life with Git easier

Or how to write a way too long title...

man pages

man git-*command*

Example: man git-commit

--patch / -p option

To see the diff of what is read

git log -> git log -p

git show -> git show -p

In "read" commands

git stash show -> git stash show -p

git stash list -> git stash list -p

To manipulate patches instead of files

Used in git add

But also with git reset, git checkout, git stash!

In "write" commands

In "write" commands

git pull --rebase

Runs git fetch + git rebase origin/your-branch
 

instead of git fetch + git merge origin/your-branch

git pull --rebase

If branch diverged, local work will be on top of remote, with no merge commit

If branch did not diverge, same as git pull

git merge --no-ff

Forces the creation of a merge commit when merging a branch in another

By default, if the branch to merge is ahead of destination, git fast-forwards the destination to the branch to merge

git merge --no-ff

git merge --no-ff

Merge of a branch without --no-ff

git merge --no-ff

Merge of a branch with --no-ff

Stash

Stash the changes in a dirty working directory away

Stash

Works like a stack

Can be popped, cleared....

To stash away your work: git stash / git stash --patch

To retrieve the most recent stash, but leave the stash unchanged: git stash apply

To remove the most recent element of your stash stack: git stash drop

To retrieve the most recent stash without leaving it in the stack: git stash pop

To clear your stash stack: git stash clear

To create a named stash: git stash save "some message"

To list your stashes: git stash list

To remove the #th stash: git stash drop stash@{#}

to see a specific stash: git stash show stash@{#}

To clear your stash stack: git stash clear

Hooks

programs you can place in a hooks directory to trigger actions at certain points in git's execution

Hooks

Scripts located in .git/hooks

Examples:
   pre-commit
   prepare-commit-msg
   post-commit
   pre-rebase
 .....

Hooks

Must:

be executable

return 0 in case of success

return != 0 in case of error

Hooks

Can:

be written in any language you fancy

Hooks

Example of hooks:

pre-commit: Check that the code about to be commited is validated by the linter

post-commit: Take a picture of you each time you commit

Hooks

Problem: Hooks are per workspace... how to automatically have them in every workspaces?

Solution: Templates!

Hooks templates

Create a folder in your computer containing all the hooks you would like to automatically have in all your projects

Run: 

git config --global init.templatedir 'path/to/the/folder/you/created'

 

To link the template directory to Git

Hooks templates

To add the hooks in existing projects, run:

 

git init

 

in the projects

Git aliases

git config --global alias.st status 

Or edit your ~/.gitconfig

git config --global alias.ba "branch -v -a"

Patches

git format-patch from-commit to-commit

Commit can be recreated with:

 

git am *patch-files*

Generates one file per commit 

Patches

git format-patch master..HEAD

git format-patch master HEAD~5..HEAD

Example:

Patches

Useful to send a change to someone so he can apply it directly, while keeping you as commiter

More

  • git notes: to annotate your commits
  • git cherry-pick: to pick a commit and add it on top of HEAD
  • git grep: To seach in your project
  • --stat, shortstat (git diff, git show, git log...)
  • git status --porcelain
  • .....

commands and options to make your life easier with Git

By Ghislain Rodrigues

commands and options to make your life easier with Git

  • 898