As a developer or anywhere relating to coding or developing stuffs for the web sitting in the web, we see or use GIT which is popular Version Control System. We see a lot of action that are repetition in nature when using with GIT or sometimes for the purpose of fixing some mistakes. So, today i would like to share some of the handy shell scripts that can be used for automating GIT stuffs for different purpose.

1. Pushing your changes to remote server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
# Description
# Integrate in your shell config file.

echo "Locating your Shell config file.. Please wait"

shrc=`find . -name "~/.zshrc" -print`
if [ -z "$shrc" ]; then
    echo "Zsh Shell wasn't found. Trying to locate Bash Shell!"
else
    echo "function gitup() { \n
    git add . \n
    git commit -a -m "$1" \n
    git push \n
}\n " >> .zshrc
fi

shrc=`find . -name "~/.bashrc" -print`
if [ -z "$shrc" ]; then
    echo "Bash shell wasn't found. Contact your owner!"
else
    echo "function gitup() { \n
    git add . \n
    git commit -a -m "$1" \n
    git push \n
}\n " >> .bashrc
fi

Usage: Running this script will add the function in the shell config file. In this scenario, i used only for two shell i.e bash and zsh. After you run the script, restart the shell and you can use the command gitup "Commit Message".

When to use: If you want to add all files at once using, i.e git add . This function will automatically add all the changed files.

Warning: It may not be good practice to add all files at once if you are working at team or want to achieve good commit history. So, please be advised.

2. Cloning all the repo at once!

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash

# Cloning all github repo at once.
# Clones all public repositories from github.

read -p 'Enter your github user-name: ' user
read -p 'Enter total page you want to clone: ' page

curl "https://api.github.com/users/$USER/repos?page=$PAGE&per_page=100" |
  grep -e 'git_url*' |
  cut -d \" -f 4 |
  xargs -L1 git clone

Usage: Execute the script anywhere you like. If you have large number of repo, make sure you run it inside a folder or it will get messy. :P

When to use: Extremely helpful if you want to download all repo at once for making changing instead of going one by one.

Warning: However, the script can’t download the private repositories. Please Google!. :O

3. Fixing your git commit author info

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@address.com"
CORRECT_NAME="Corrected Name"
CORRECT_EMAIL="your-correct-email@address.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Usage: Execute the script files at the root of the git repo where you would like to fix the commit author info. Then push the changes to the server by git push --force --tags origin 'refs/heads/*'

When to use: If you have used different email address for commits and want to use only one. Using different email may not show all the contribution in your profile.

Warning: May not suitable for project or work with different member.

I will be updating this list frequently as possible. However, if you found this blog post informative and useful and want to add or suggest your own git script, head over here in the Github. Fork it and then submit for PR. I will be more than happy. Thanks for the time. We will meet in next blog post!