Git & Shell Script

Dongwon Jeong·2023년 4월 15일
0

GDSC Yonsei

목록 보기
1/3
post-thumbnail

Git

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 user.name "firstname lastname"

#set an email address that will be attached to your commits
git config --global user.email "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]

git_add

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]

git_commit

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"

git_push

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]

remote

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_hook

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")

pre_commit

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.

#!/bin/sh

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

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

pre_push

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

#!/bin/sh

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

remote="$1"
url="$2"

z40=0000000000000000000000000000000000000000

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

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

exit 0

.git

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.

.git/hooks

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.

ls

Prints a listing of a specific file or directory.

ls [path]

pwd

Prints the user's current working directory.

pwd

cd

Changes the current working directory.

cd [path]

sudo

Run command with the security privileges of the super user.

sudo [command]

vi

Standard command-line text editor.

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

#To enter the insert mode
i

#To go back to command mode, press Esc key

#To save and quit
:wq

#If no changes are to be saved, exit with
:q

vim

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

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

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

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

zsh

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 scriptname.sh

#Change the permissions
sudo chmod 744 scripname.sh

#To run the script
<scriptname.sh>

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

chsh

Change your default shell.

chsh

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

.bashrc

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

.zshrc

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

source

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

#To run the shell script file using 'source'
source scriptname.sh

#To run the shell script file using '.'
. scriptname.sh

.profile

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.

chmod

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>

chown

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

chown <user>:<group><file>

ln

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개의 댓글