Version control using Git & GitHub : a must for a graduate student
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
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
For resolving the conflict between Windows and Unix users about LF and CRLF character, use this2 in Win- dows
Useful commands:
Difference between two files -
Git:
History of file:
To see commit history -
Difference between two specific commits
File log for each commit
Cloning repository:
or,
To check out previous commit
This might give you detached HEAD
error message. To fix this
Creating repository:
With existing files in a directory
Commit:
Add file in staging area
Now, if you want to remove one FILE2
from staging area-
Once files are in staging area, to commit them-
which will open up a file in vim
Sample commit message guide can be found here. Staging and committing together (for existing file modification and deletion not for new files).
To check difference between working directory and staging area-
To check difference between staging area and repository-
If you want to get rid of changes in working directory and staging area
Branching:
master
is normally the main branch. To create new branch.
We re still in master
branch. To work on new branch we have to check it out.
These two commands cane be written in one single command-
To see commit history on different branch in a graphical way
To compare a commit to its parent
Merging:
Lets say we want to merge BRANCH1
in to BRANCH2
.
Now BRANCH1
is already been merged in to BRANCH2
. We don’t need that label anymore. To delete that branch (label)
If something goes wrong and you want to go back to the state before start merging git merge –abort
Conflict:
Lets say we want to merge BRANCH1
in to BRANCH2
.
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 -
To add remote (origin
is used as REMOTE_NAME
in general)-
To see pushing and fetching url -
Push:
To push local branch to remote (for example REMOTE_NAME
=origin
and LOCAL_BRANCH_NAME
=master
)
Pull:
To pull remote in to local machine (for example REMOTE_NAME
=origin
and BRANCH_NAME
=master
)
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
It is same like writing these two commands -
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
)
Update the upstream remote.
Then merge this master to your development branch
Updating forked repository:
Adding Main repository as upstream
Merging one branch to upstream/branch
Updating GitHub online repository:
Stashing:
To suspend current work temporarily
To see what is in stash branch
Bring back stashed files