BLOG | A guide for Git command

I. Setup

In Mac Terminal, type “git init” before clone to init a new repository.

git init                               // to create a new repoitory

To clone a repository to local from Github/remote server:

git clone /path/to/repository          // clone repo to local

// clone to local from remote server 
git clone username@host:/path/to/repository

There are three trees maintained by git – working directory, index and head. Files are stored into your working directory, and then added to your index tree. Finally with a commit command files are committed into the head tree.

git add <filename>             // add file into Index
git add .                      // add current folder contents into Index
git commit -m "Commit Msg"     
// now files are in Head but not your remote repo yet

To update contents in your remote repository, use push command.

git push origin master        // push changes into actual repo
// you can also push to your own branch 

You can also connect your repository to a remote server and push later changes to the server (if you haven’t cloned your repo to local):

git remote add origin <server>     // connect repo to remote server 

II. Branch

In Git, branches are used to develop features isolated from each other. Your default branch is the “master” branch when create a repository. You can use other branches to develop features and merge them back to the master branch later.

git checkout -b new_branch_name    // create new branch and switch to it
git checkout master                // switch back to master
git branch -d new_branch_name      // delete the branch 
git push origin <branch>           // push and make branch visible

New branch is not available to others unless be pushed to the remote repository.

III. Update & Merge

Use pull command to update your local folder with contents in your remote repository.

git pull                         // get contents from remote 

Use merge to merge new branches to master.

git merge <branch>               // have to manually edit sometimes 
git add <filename>               // after merge mark file as merged 
git diff <source_b> <target_b>   // before merging preview 

IV. Log and Tag

Create tags for software releases using:

git tag 1.0.0 first_10_char_of_commit_id

You can find commit id use

git log

If you want to edit log information:

git log --author=bob           // see only commits from bob
git log --pretty=oneline       // see each commit in one line 
// see an ASCII art tree of all branches
git log --graph --oneline --decorate --all
git log --name-status          // see only files that have changed
git log --help                 // see help

V. Replacement

git checkout -- <filename>   // replace a file with last content in Head
// changes added to Index will be kept

If you want to drop all local changes and commits and fetch the latest history:

git fetch origin
git reset --hard origin/master

Other:

gitk                                // build in GUI
git config color.ui true            // use colorful git output
git config format.pretty oneline    // one line per commit 
git add -i                          // interactive adding 

This guide is a walk through for the excellent blog here: https://rogerdudler.github.io/git-guide/.

Leave a comment