Git

Git #

Git is one of the most important software ever created. It enabled people to work geographically distributed even with intermittent Internet connection. Each contributor has a copy of the repository including the historical changes. Software evolved faster than ever.

To update the local list (cache) of remote branches #

# To show all local and remote branches that (local) Git knows about:
git branch -a
# And to update it:
git remote update origin --prune
# Now you can see everything up to date with the origin
git branch -a

Or you can also do a git pull --all to fetch from origin and update all your local branches.

git patch #

CommandDescription
git format-patch -10 --stdout > mypatch.patchCreates a patch file with the last 10 commits.
git apply mypatch.patchApplies the patch file.
git apply --check mypatch.patchChecks if the patch can be applied.
git stash list; git stash show -p stash@{<number>} > mypatch.patchCreates a patch file from a stash.
git diff > mypatch.patchCreates a patch file from uncommited changes.

git stash #

Everything is stashed in a stack. To reference it you can do stash@{n} where n is the offset in the stack.

To stash it you can do:

# stashes everything
git stash
# list what is stashed
git stash list
# apply the most recent stash and delete it
git stash pop # it's the same as doing git stash pop stash@{0}
# apply the second most recent stash and delete it
git stash pop stash@{1}
# apply the stashed content and does not delete it like pop does
git stash apply # it's the same as doing git stash apply stash@{0}
# you can set a description to stashed content
git stash save "message"
# you can view a summary of a stash
git stash show
# or to show the full diff
git stash show -p

Creating a branch from a stash #

git stash branch add-stylesheet stash@{1}

Cleaning up the stash #

git stash clear
#or
git stash drop # it will drop the stash@{0}

Changing the author of the last commit #

git commit --amend --author="Eduardo Ivan Pichler <eduardo.pichler@myemail.com>" --no-edit

Changing the Author for global and local repositories #

git config --global user.name "João"
git config --global user.email "joao@myemail.com"

It can also be local instead of global.

git commit #

Signing your commits #

Congigure the key to be used: git config --global user.signingkey EUUU75A0F4CD1D98FC863AAB9AFEA220BB696AA1

Do git config --local commit.gpgsign true in your repository.

git push #

Auto setup the remote branches #

git config --global 'push.autoSetupRemote' true

Pushing automatically after a commit #

You need to have an executable (chmod +x) file in .git/hooks/post-commit that contains the following:

#!/bin/sh
git push origin master

You can create it by doing:

FILE=".git/hooks/post-commit"
if [ ! -f $FILE ]; then
  touch $FILE
  echo '#!/bin/sh' | tee $FILE
fi
echo 'git push origin master' | tee -a $FILE

git log #

Software archeology with git.

CommandDescription
git shortlog -sne | cut -f2 | sortShows the authors of the commits
git log --onelineShows the commit hash and the commit message
git log --oneline --author=carlosShows the commits of a specific author
git log --statShows the commit hash and the files changed
git log --oneline --graph --all --decorateShows the commit hash and the branches
git log --oneline --graph --all --decorate --simplify-by-decorationShows the commit hash and the branches simplified
git log --no-merges --no-renames --numstat --pretty=format:"" -- **/*.java | cut -d$'\t' -f3 | grep -v '^$' | sort | uniq -c | sortCount the number of commits per file. Files with many commits can often, but not always, point to a design problem.
git shortlog -ns -- **/*.javaShows the number of commits per author in a specific file
git shortlog -ns . --since "5 month ago"Shows the number of commits per author in the last 5 months
git grep --perl-regexp "\/\/ *(todo| fixme)"Shows the todos and fixmes in the code
git log --pretty=format:"%h %s"Browse through the commit messages
git log --author=edu --pretty -G \.stream\(\)Shows the commits of a specific author that contains a regex.
git log --author=edu --pretty -G \.stream\(\) -- **/*Test.javaShows the commits of a specific author that contains a regex filtering by specific files.

Commented code:

git grep --perl-regexp " \/\/.*(=|;)" -- *.java

Count last changed line in each Java source code file per author #

find . -type d -name ".git" -prune -o -type f \( -iname "*.java" \) | xargs -n1  git blame -w -f -C -M  --date=format:"|" | cut -d"|" -f 1 | cut -d"(" -f 2 | sed 's/\s*$//' | sort | uniq -c | sort

Search in the commit content #

 git rev-list --all | xargs git grep \.stream\(\)

Or:

 git grep stream\. $(git rev-list --all)

In git grep you can have the line number by adding -n.

Or :

git log -Gstream

You can add a -p flag:

git log -p -Gstream

See more ways in this forum

Limiting to a subtree, for instance lib/util. Notice you need to pass it in both commands: #

git grep <regexp> $(git rev-list --all -- lib/util) -- lib/util

Search working tree for text matching regular expression regexp: #

git grep <regexp>

Search for a pattern only in the changed lines #

git diff --unified=0 | grep <pattern>

Search all revisions for text matching regular expression regexp: #

git grep \.stream\(\) $(git rev-list --all)

git rebasing #

CommandDescription
git fetch && git fetch origin tag my-tag –no-tags –forceFetch the my-tag from the origin
git rebase my-tag; git checkout -b feature/branch-nameRebase the current branch to the my-tag and create a new branch from it.

git branch #

CommandDescription
git branch -aShows all the local and remote branches that the local git know about
git branch -a --contains 8beeff00dShows the branches that contain a commit
git branch -d branch_nameDeletes a branch
git branch -m old_branch new_branchRenames a branch
git branch -vvShows the tracking branches

git show #

CommandDescription
git show 6988bec26b1503d45eb0b2e8a4364afb87dde7afShows the details of a commit

git filter-branch #

Rewriting branches history.

CommandDescription
git filter-branch --tree-filter 'rm -rf terraform/*.tfstate'Remove all the .tfstate files from the repository history

git rm #

CommandDescription
git rm -r –cached folderRemoving a file whitout deleting it locally.