Pages

Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Sunday, July 20, 2014

Script to find repositories with unstaged/uncommited changes

I found this nice script that I often use to check if I did not forget to push the latest changes to git.

Here are some instructions on how to use it:
  1. Save the snippet below to a file and name it something like multipleStatus.sh;
  2. Place the file on the same folder of your repositories;
  3. To run the script, browse to that folder and type:
    source multipleStatus.sh; find-dirty

And here is the snippet you need to save:
#!/bin/bash

function unstaged_changes() {
    worktree=${1%/*};
    git --git-dir="$1" --work-tree="$worktree" diff-files --quiet --ignore-submodules --
}

function uncommited_changes() {
    worktree=${1%/*};
    git --git-dir="$1" --work-tree="$worktree" diff-index --cached --quiet HEAD --ignore-submodules --
}

function find-dirty () {
    for gitdir in `find . -name .git`;
    do
        worktree=${gitdir%/*};
        if ! unstaged_changes $gitdir
        then
            echo "unstaged     $gitdir"
        fi

        if ! uncommited_changes $gitdir
        then
            echo "uncommitted  $gitdir"
        fi
    done
}

Monday, June 2, 2014

Remove unwanted file that has been committed several commits ago from git repository

Recently I have been developing a contacts management program and as usual I've been using git for source code management. This program stores all the contacts info in a file, say contacts.info.

Today I bumped into a problem I had never had:
I noticed I had committed contacts.info several commits ago. As you can imagine, I had no intention of making those contacts info public. I needed to delete that file from my git repository, from all the commits I've committed since I added contacts.info.

I ended up finding a very useful tool for the job: BFG-Repo-Cleaner.

If you have a similar problem, here is a step-by-step guide on how to completely remove an unwanted file that has been committed several commits ago from your git repository:
  1. Download BFG-Repo-Cleaner;
  2. Clone a bare copy of your repository:
    • git clone --mirror https://github.com/difusal/my-repo.git
  3. Delete contacts.info from all the commits:
    • java -jar bfg-1.11.6.jar --delete-files contacts.info my-repo.git/
  4. Push the repository to save changes:
    • git push
Yes, it is just that simple! If you browse through your past commits, any trace of contacts.info should now have been completely deleted.