Fixing the “protocol error: bad pack header” error in git

When trying to push in git, you might get the following error message: fatal: internal server error remote: internal server error fatal: protocol error: bad pack header In my case, it was because I pulled a corrupted remote Gerrit repository, and tried to push the corrupted data back to the fixed remote. Everyone who had […]

Automatically tagging people for code review in Gerrit

If your gerrit workflow requires you to automatically tag people, or if you always end up tagging the same people, there is a way to tag people by default for all your commits. To do so, edit .git/config to include the following lines and substitute the email with your colleague’s email: [remote "origin"] … receivepack […]

Deleting directories while preserving symlinks

At work, we needed to replace a pre-existing folder with a symlink. We wanted to symlink the user’s .git/hooks folder to ../build/git-hooks every time the script was run. We wanted to delete .git/hooks if it existed and replace it with a symlink, but we also wanted to avoid deleting ../build/git-hooks‘s contents by accident if the […]

Multiple git stashes

Did you know git can keep multiple stashes? If you use git stash and git stash apply, you might be tempted to think there is a single stash, but you can restore older stashes, and thus stash multiple items. To see your previous stashes, use git stash list. You will get a result like this: […]

A git cheat sheet to get you started

Here is a list of useful git commands I have compiled over the past few years. Although you should absolutely learn git the hard way by making mistakes, breaking things and swearing at your terminal, you might need to get things done now. Here is a git cheat sheet that will get you through most […]

Get the number of lines of code for a project

If you want to get the total line count for a project, you can simply combine the find and wc commands. In the example below, we get the number of lines of code for all PHP, JavaScript and CSS files under a given directory: find [directory] -name ‘*.php’ -o -name ‘*.js’ -o -name ‘*.css’ | […]

How to see how many lines of codes were changed in a day with git

If you want to know how many files and how many lines of code were changed for the day, simply run the following command: git diff –stat “@{1 day ago}” This will output a list of all changed files along with the number of lines added and removed, as well as a total for all […]

Getting the hash of the first commit in git

If you want to know what your oldest git commit is, use the following command to return its hash: git rev-list HEAD | tail -n 1 You can then use git show to find the commit date, author and message: git show –quiet [your hash] To show the diff for that first commit, remove the […]

How to push all branches in git

If you want to push all branches in your repository when you type git push, type the following commands: git config –add remote.origin.push ‘+refs/heads/*:refs/heads/*’ git config –add remote.origin.push ‘+refs/tags/*:refs/tags/*’ This will tell git to push everything including branches that were never pushed when you don’t specify what to push.

Create a global .gitignore in git

Are you tired of adding .DS_Store, .idea and other files to the .gitignore of all of your projects? Git lets you specify a universal .gitignore file to ignore them once and for all. It’s a rather simple command: git config –global core.excludesfile /path/to/global/.gitignore Pretty simple, isn’t it?