This tutorial is based on an online course provided by Udacity. Here we will learn about basic commands to manage git repositories offline and online.

Rules:

Capital letters will be used for variables.

Configuring Git:

To highlighting red and green in git diff command, use this

$ git config --global color.ui auto

Download git-completion.bash file and move it to home directory. Download git-prompt.sh file and move it to home directory. Download this file .bash_profile and add the line to .bash_profile at home directory. Default editor set to vim

$ git config --global core.editor "vim"
$ git config --global push.default upstream
$ git config --global merge.conflictstyle diff3

For resolving the conflict between Windows and Unix users about LF and CRLF character, use this2 in Win- dows

$ git config --global core.autocrlf true

Useful commands:

Difference between two files -

$ diff -u FILE1 FILE2

Git:

History of file:

To see commit history -

$ git log

Difference between two specific commits

$ git diff COMMIT_ID1 COMMIT_ID2

File log for each commit

$ git log --stat

Cloning repository:

$ git clone URL

or,

$ git clone URL FOLDER_NAME

To check out previous commit

$ git checkout COMMIT_ID

This might give you detached HEAD error message. To fix this

$ git checkout BRANCH_NAME

Creating repository:

With existing files in a directory

$ cd DIRECTORY
$ git init

Commit:

Add file in staging area

$ git add FILE1
$ git add FILE2

Now, if you want to remove one FILE2 from staging area-

$ git reset FILE2

Once files are in staging area, to commit them-

$ git commit

which will open up a file in vim

$ git commit -m "COMMIT_MESSAGE"

Sample commit message guide can be found here. Staging and committing together (for existing file modification and deletion not for new files).

$ git commit -a -m "COMMIT_MESSAGE"

To check difference between working directory and staging area-

$ git diff

To check difference between staging area and repository-

$ git diff --staged

If you want to get rid of changes in working directory and staging area

$ git reset --hard

Branching:

master is normally the main branch. To create new branch.

$ git branch NEW_BRANCH_NAME

We re still in master branch. To work on new branch we have to check it out.

$ git checkout NEW_BRANCH_NAME

These two commands cane be written in one single command-

$ git checkout -b NEW_BRANCH_NAME

To see commit history on different branch in a graphical way

$ git log --graph --oneline BRANCH1 BRANCH2

To compare a commit to its parent

$ git show COMMIT_ID

Merging:

Lets say we want to merge BRANCH1 in to BRANCH2.

$ git checkout BRANCH2
$ git merge BRANCH2 BRANCH1

Now BRANCH1 is already been merged in to BRANCH2. We don’t need that label anymore. To delete that branch (label)

$ git branch -d BRANCH1

If something goes wrong and you want to go back to the state before start merging git merge –abort

$ git merge --abort

Conflict:

Lets say we want to merge BRANCH1 in to BRANCH2.

<<<<<<<< HEAD
// current branch code
|||||||| merged common ancestors
// common code
========
// other branch code
\>>>>>>>> master

GitHub:

As GitHub is an online repository, adding ssh key for your local machine helps a great deal. It can be done from here.

Adding Remote Repository:

To check the remote repositories -

$ git remote

To add remote (origin is used as REMOTE_NAME in general)-

$ git remote add REMOTE_NAME URL

To see pushing and fetching url -

$ git remote -v

Push:

To push local branch to remote (for example REMOTE_NAME=origin and LOCAL_BRANCH_NAME=master)

$ git push REMOTE_NAME LOCAL_BRANCH_NAME

Pull:

To pull remote in to local machine (for example REMOTE_NAME=origin and BRANCH_NAME=master)

$ git pull REMOTE_NAME BRANCH_NAME

It makes a node like REMOTE_NAME/BRANCH_NAME (for example origin/master). Once you push any change to remote from local, this node is also update.

Merging remote changes:

Fetching remote repository and mergin it to local branch can be done with single command

$ git pull REMOTE_NAME BRANCH_NAME

It is same like writing these two commands -

$ git fetch REMOTE_NAME
$ git merge BRANCH_NAME REMOTE_NAME/BRANCH_NAME

For fetch command by default the REMOTE_NAME is origin.

Normal process of development is - you make a branch, work on it, commit changes locally, push the new branch to remote, and then send a pull request to the collaborator. Make sure you make the request to merge your branch to the master.

Pull request conflict:

Add another remote (adding original repo not the forked repo, in general REMOTE_NAME is named as upstream)

$ git remote add REMOTE_NAME URL

Update the upstream remote.

$ git pull upstream master

Then merge this master to your development branch

$ git checkout DEVELOPMETN_BRANCH
$ git merge master DEVELOPMETN_BRANCH
$ git push origin DEVELOPMENT_BRANCH
$ git checkout master
$ git push origin master

Updating forked repository:

Adding Main repository as upstream

$ git remote add upstream URL
$ git fetch upstream

Merging one branch to upstream/branch

$ git checkout BRANCH_NAME
$ git merge upstream/BRANCH_NAME

Updating GitHub online repository:

$ git push origin BRANCH_NAME

Stashing:

To suspend current work temporarily

$ git stash

To see what is in stash branch

$ git stash list

Bring back stashed files

$ git stash apply