Git

Publish Date: Apr 25, 2018    Last Update: Oct 29, 2018

Basic

Configurations

# List current configurations
git config --list 
# Modify configurations
git config -e [--global]

# Modify personal info
git config --global user.name "XXX"
git config --global user.email "XXX"

Basic workflow

The basic steps to create a repository:

  1. Initiate the project

    git init                // in the current folder
    git init [project name] // create a folder
    
  2. Add and delete files

    # Add files 
    git add [file1] [file2]
    git add [dir]
    git add .
    # Delete files
    # Delete files both in the workspace and index
    git rm [file1] 
    # Stop tracking but keep in workspace
    git rm --cached [file]
    # Change the file name
    git mv [oldName] [newName] 
    
  3. Commit the index to repository

    # Commit all files in the index
    git commit -m [message] 
    # Commit certain files in the index
    git commit [file1] [file2] -m [message]
    # Commit all changes after last commit
    git commit -a
    # Commit with all diff information
    git commit -v
    # Amend the last commit
    git commit --amend -m [message]
    

Add remote repository

git remote add origin git@bitbucket.org:xxx/xxx.git

Utilize Branches

  1. Create new branch

    # List all local branch
    git branch
    # List all remote branch
    git branch -r
    # List all branch
    git branch -a
    # Create a new branch
    git branch [newBranch]
    # Create a new branch and checkout it
    git checkout -b [newBranch]
    # Checkout to a branch
    git checkout [branch]
    
  2. Local and remote branch We push or pull the branches to remote with

    # Push a branch to remote
    git push origin [branch]
    # Pull the local branch
    git pull [remote] [branch]
    

Further, we can checkout a remote branch with track

# Create a branch and track with remote
git branch --track [localNew] [remoteBranch]
  1. Merge branches

    git checkout master
    # switch to the master branch
    git merge dev
    # merge the development
    
  2. Delete branch

    git branch -D dev
    # delete the local branch dev
    git push origin --delete dev
    # delete the remote branch dev
    

Status Checking

# Show the changed files
git status
# Show the version history of current branch
git log
# Show the version history of a file
git log --follow [file]
# Show the difference between index and workspace
git diff
# Show the content change
git show [commit]

Remote repository

# Download all change
git fetch [remote]
# Show all remote repository
git remote -v
# Add a remote
git remote add [name] [url]
# With branch
git pull [remote] [branch]
git push [remote] [branch]
# Push all branches to remote
git push [remote] --all

Advanced

Git Repository on a server

Server Side

First thing is that we need to set up git repository

git init --bare

Since I am using git to manage my wiki and blog system, it is important to set up a post-receive action, which copies the git repository to the /var/www folder. To do this, a typical post-receive doc is written as

#!/bin/bash
GIT_REPO=/home/xxx/GIT/wiki
TMP_GIT_CLONE=/tmp/wiki
PUBLIC_WWW=/var/www/wiki
rm -rf ${TMP_GIT_CLONE}
git clone ${GIT_REPO} ${TMP_GIT_CLONE}
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}

This post-recieve file is put in the git/hooks/ folder.

Client Side

Usually the port is not set at the default port, therefore we need to modify the .ssh/config file to change the port

host HOST
hostname xxxx.com
port PORT

Then we can download the git using

git clone user@HOST:git/xxx

Tricks

Clone with submodule

git clone git://github.com/lipop/vim.git
cd vim
git submodule init
git submodule update

Use SSH to connect to github

  1. Add your SSH key to the github website, following the wiki
  2. Clone the original project to your computer
  3. Modify the .git/config file: replace the url to git@github.com:lipop/xxx
  4. Using ssh-agent: ssh-add .ssh/id_rsa

Keep an empty folder

Create a .gitkeep file in the folder will do the job.

To Delete a file from Tracking

Sometimes we may accidentally add a file to tracking. To delete the file from tracking:

# First remove the cached files
git rm --cached <FILE>
git rm --cahced -r <FOLDER>
# Push to origin
git commit -m "Give up the <FILE>"
git push
# Use gitignore
vim .gitignore
git add .
git commit -m "Adding ignore file"
git push

To unstage the changes

Sometimes you may mistakenly use add .. You can unstage the changes with

git reset HEAD <FILE>