Git & Shell Script

prsper2001·2023년 4월 15일

GDSC Yonsei

목록 보기


Git is a free and open source distributed version control system(VCS).

To set up Git for the first time, use the following command lines.

#set a name that will be attached to your commits
git config --global "firstname lastname"

#set an email address that will be attached to your commits
git config --global "valid-email"

#set automatic command line coloring for Git for easy reviewing
git config --gl0bal color.ui auto

You can initialize/clone repositories from GitHub using the following comand lines.

#initializing an existing directory as a Git repository
git init [project name]

#clone an entire repository from a URL
git clone [project url]


You can add a file to the staging area. Use in place of the full file path to add all changed files from the current directory down into the directory tree.

git add [file]


You can create a new 'commit' from changes added to the staging area. You also have to add a message along with the commit.

git commit -a -m "message"


You can push local changes to the remote.

#Use --tags to push tags
git push [--tags] [remote]

#Set local branch's copy as an upstream
git push -u [remote name] [branch name]


You can create, view and delete connections to other repositories.

#Create a new connection to a remote repository. Use [name] as a convenient shortcut for [url].
git remote add [name] [url]

#Remove the connection to the remote repository called [name]
git remote rm [name]

#Rename a remote connection from [old name] to [new name]
git remote rename [old name] [new name]


Git hooks are shell scripts found in the hidden .git/hooks directory of a Git repository. These scripts trigger actions in response to specific events, so they can help you automate your development lifecycle. They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle.
Hooks can resite in either local or server-side repositories, and they are only executed in response to actions in that repository.
.sample extension prevents hooks from executing by default. So, removing the .sample extension is how a hook is installed.
The shebang line (#!/bin/sh) in each script defines how your file should be interpreted. For instance, we can write an executable Python script in the prepare-commit-msg file instead of using shell commands.

#!/usr/bin/env python

import sys, os

commit_msg_filepath = sys.argv[1]
with open(commit_msg_filepath, 'w') as f:
    f.write("# commit message")


A client-side git hook that is exectued every time you run 'git commit' before it asks the developer for a commit message or generates a commit object. It can be used when you want to run some automated tests that make sure the commit doesn't break any existing functionality.


# Check if this is the initial commit
if git rev-parse --verify HEAD >/dev/null 2>&1
    echo "pre-commit: About to create a new commit..."
    echo "pre-commit: About to create the first commit..."

# Use git diff-index to check for whitespace errors
echo "pre-commit: Testing for whitespace errors..."
if ! git diff-index --check --cached $against
    echo "pre-commit: Aborting commit due to whitespace errors"
    exit 1
    echo "pre-commit: No whitespace errors :)"
    exit 0


A client-side git hook that runs right before a reference is pushed to a remote 'git push'.


# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done



IFS=' '
while read local_ref local_sha remote_ref remote_sha
	if [ "$local_sha" = $z40 ]
		# Handle delete
		if [ "$remote_sha" = $z40 ]
			# New branch, examine all commits
			# Update to existing branch, examine new commits

		# Check for WIP commit
		commit=`git rev-list -n 1 --grep '^WIP' "$range"`
		if [ -n "$commit" ]
			echo "Found WIP commit in $local_ref, not pushing"
			exit 1

exit 0


Using the .git method will make hooks local to your machine only. If you wanted other users working on your project to use the git pre-push hook, they would need to set it up locally as well.


Shell Script

Shell is a program whose primary purpose is to read commands and run other programs. This blog specifically uses Bash, the default shell in many implementation of Unix.


Prints a listing of a specific file or directory.

ls [path]


Prints the user's current working directory.



Changes the current working directory.

cd [path]


Run command with the security privileges of the super user.

sudo [command]


Standard command-line text editor.

#To run vi (command mode is default)
vi <file>

#To enter the insert mode

#To go back to command mode, press Esc key

#To save and quit

#If no changes are to be saved, exit with


Terminal based text editor. That stands for vi improved. It contains new features such as windows splitting, tabs, code highlighting, macros, undo and redo multiple times, command line history, pattern matching, and word completion.

To run vim
vim <file>


Bash script is a collection of Bash commands which are kept in a text file.

#Create a bash script by placing
#at the top of the file.

#Change the permissions on the file to make it executable
chmod u+x [script name]


A text file that contains instructions or commands to be executed by the ZSH shell. ZSH shell is an extended version of the Bourne Again Shell. So, most commands and scripts written for bash work on ZSH.

#To execute permissions for your shell script
sudo chmod +x

#Change the permissions
sudo chmod 744

#To run the script

#If the script accepts any arguments, pass them on as such
<> <arg1> <arg2>...<argN>


Change your default shell.


#Change to fish shell
chsh -s /ser/bin/fish


Script file that's executed when a user logs in. It contains a series of configurations for the terminal session.

#To view the file
ls -a

#To view the bashrc file
cat .bashrc


Configuration file that contains the commands that run the ZSH shell, just like the .bashrc file that contains commands for the bash shell.


Used to execute a shell script file. It has the same function as '.'.

#To run the shell script file using 'source'

#To run the shell script file using '.'


During an interactive shell login, if .bash_profile is not present in the home directory, Bash looks for .bash_login. If found, Bash executes it. If .bash_login is not present in the home directory, Bash looks for .profile and executes it.
.profile can hold the same configurations as .bash_profile or .bash_login. It controls prompt appearance, keyboard sound, shells to open, and individual profile settings that override the variables set in the /etc/profile file.


Change the permission of a file or folder.

#Change the permission of a file to 755
chmod 755 <file>

#Change the permission of a folder and its contents to 600
chmod -R 600 <dir>


Change the ownership of a file to user and group. Add -R to include folder contents.

chown <user>:<group><file>


Create linkings between files. By default, it creates hard links.

#default hard link
ln <file> <reference>

#soft link
ln -s [OPTIONS] <first argument> <second argument>

0개의 댓글