WRITELOOP

MORE GIT TIPS

2018 April 20

How to edit git configuration for a repository (the easy way):

Instead of manually editing [project]/.git/config, you can, inside the repository, use git config -e.

Hooks

Hooks are a nice way to automate those things you tend to forget (remove “pdb” breakpoints, run your test suite, etc). Every repository under the .git directory has a folder named hooks, where you have some examples for each one of the available hooks. To create a hook, you must create a file under this directory with the name of the stage you want to hook on, based on the ones available on this directory, without the .sample suffix.

Como exportar um repositório do hg (mercurial) para o git:

$ git clone https://github.com/frej/fast-export $ mkdir git_repo_to_keep_the_hg_clone $ cd git_repo_to_keep_the_hg_clone $ git init $ ../fast-export/hg-fast-export.sh -r [full_path_of_hg_repo] –force $ git checkout $ git status O git status não deve mostrar nada a ser feito. Após isso, basta criar um repositório em branco com o mesmo nome no github, bitbucket, gitlab, e finalmente adicionar o remote nessa cópia local do repositório. Ex.: $ git remote add origin https://github.com/githubuser/cool_repository.git $ git push -u origin master

How to work on a fork of a github repository:

Fork a repository on github. Example: https://github.com/githubuser/cool_repository. The idea is that we will work on any branch of our forked repository and, when ready, we will merge the changes to our fork’s master. Then we make a pull request from our fork’s master to githubuser/cool_repository master branch. Your forked repository remote is called “remote origin”, and the githubuser official repository remote is called “remote cool_repository”.

WORKFLOW

On your repository (remote origin) you can do whatever you want and even work on your separate branches. After finishing up, you must consolidate all of your changes into your master branch. After that, you must make a pull request on Github to githubuser/cool_repository. Your work will be reviewed and then rebased into githubuser/cool_repository master branch, which then will be ready to be deployed on production. To get the master branch from your local fork: $ git pull origin master To get the master branch from the official githubuser/cool_repository: $ git fetch cool_repository $ git rebase cool_repository/master Your git config file (found on the project root, under “.git/config”) must be like below - just change “tiagoprn” to your Github’s account name: [core] repositoryformatversion = 0 fileMode = false bare = false logallrefupdates = true [remote “origin”] url = https://github.com/tiagoprn/cool_repository.git fetch = +refs/heads/:refs/remotes/origin/ [branch “master”] remote = origin merge = refs/heads/master [remote “cool_repository”] url = https://github.com/githubuser/cool_repository.git fetch = +refs/heads/:refs/remotes/cool_repository/

Git aliases:

No .git/config do seu repositório, acrescente no final do arquivo: [alias] br = symbolic-ref –short HEAD hist = log -10 –pretty=format:'%h %ad | %s%d [%an]' –graph – To use: git br git hist

Remove a large file from the git history:

(This can also be used to remove ANY FILE from the git history, definitively and once and for all) $ git filter-branch –prune-empty –index-filter ‘git rm -rf –cached –ignore-unmatch FULL/FILE/PATH/EQUIVALENT/TO/THE/PROJECT/ROOT’ –tag-name-filter cat – –all reference: http://naleid.com/blog/2012/01/17/finding-and-purging-big-files-from-git-history E.g.: $ git filter-branch –prune-empty –index-filter ‘git rm -rf –cached –ignore-unmatch python/get_product_zip_feed_from_cnova_ftp/CB_XML_Precifica.XML’ –tag-name-filter cat – –all

How to promote another branch to master - or - how to make a new master from a past commit

That could be used, e.g., to easily revert back changes made to master to a previous point in time. What can be done in that case is to checkout an old commit hash and create a branch called, e.g, “PAST” to it: $ git checkout -b PAST 9e53f78f464ceb11ef25a069a596ba40b483f634 (you can, of course, checkout any other branch you wish, don’t restrict yourself to a previous commit ;) $ git merge –strategy=ours master # keep the content of this branch, but record a merge $ git checkout master $ git merge PAST # fast-forward master up to the merge $ git push origin master $ git branch -d PAST # deletes the branch used to go back

How to see the files added/removed/changed from the last 15 days on:

$ git diff –stat @{15.days.ago}

Git log on one line:

$ git log –pretty=oneline

TAGS