Github commands

Github

A version control system keeps track of collection of files allows to revert to any other version. Each version captures a snapshot of the files at a certain point of time.

These files with source code of programming languages are stored in the repository.

Stored in any of the public or private repository .

Install git-hub in Linux

sudo apt-get install git  

sudo apt-get install git-core  

Install git-hub in Windows

Download from the url => http://github-windows.s3.amazonaws.com/GitHubSetup.exe


Setup User configuration

Configure user and email for github

git config --global user.name "pandubalraj"

git config --global user.email "pandubalraj@gmail.com"


Clone

The user can copy the existing repository. This process is called cloning.

Git is a Distributed version control system – each cloned repository is the full copy of repository

Git originates from the Linux kernel system implemented in C. Ability to support other languages like Java, Ruby, Python.

Clone remote repository

git clone <repository git-hub Http URL>


Remote

Git allows user to synchronize the local repository with the other (remote) repository.

User can push changes to the remote repository (if authorized by owner). Fetch the latest changes in the remote repository.

In order to list the remote repository

git remote
---> list the remote branch

git remote -v
---> list the remote branch with the git-hub URL


Branching

User can able to create,delete branches in local repository. Work on them and later on able to push or fetch it to the remote.

Create a branch

git branch -a
---> list all the local and remote branches

git branch
---> list the local branches

git branch <branchname>
---> create a new branch with name given

git checkout <branchname>
---> move to the other branch

git checkout -b <branchname>
---> create branch and switch to it Rename branch 
git branch -m [old_name] [new_name]


Add

The new files need to be first added in the repository. These new files are listed below the untracked files.

Add a file

git add <filename>


Commit

In order to add permanently the files to the git repository. The added files need to be committed. Commitment creates a new snapshot of the working tree in the repository.

Committing a file

git commit -m “commit message” <filename which needs to be committed>


Commit object comprises of
Commit ID
Tree Object ID
Authors
Committer
Commit message


Add and Commit in a single command

git commit -a -m "changes in file"


Mergetool

Some of the merge tools are tortoisemerge, p4merge, kdiff3

git mergetool

Install kdiff3
sudo apt-get install kdiff3

Change the default merge-tool to other as

git config --global merge.tool kdiff3


Merge conflict

To merge the conflicts first use third party tool mentioned above. Then remove the unwanted lines. Commit the latest or do empty commit if there is no changes.

git mergetool

git commit



Create git repository

Create a git repository and add all the files

git init

git add .
---> add all the files in the current path


Result

Use git log command to view the committed files

git log --oneline
---> the output will be in single line

git log

Shows the sample output log

commit 93f226eed2f23a9d4520123234234342sd234
Author: pandubalraj <pandubalraj@gmail.com>
Date: Thu Jun 13 18:30:42 2013 +0530
initial commit


Remove the files and record in git-hub

git rm <filename>

Files can be removed in local and the deleted files can be committed in remote git-hub


Current status

git status


Push current changes to remote

git push origin master

---> push the latest committed files to the remote (master branch – default)


Tagging in Github

#list tag
git tag


# create lightweight tag 
git tag 1.7.1  

# See the tag 
git show 1.7.1 

  # to explicitly push a tag and not a branch 
  git push origin tag <tagname>  

# delete tag locally 
git tag -d 1.7.0  

# delete tag in remote repository called origin 
git push origin :refs/tags/1.7.0  


Other frequently used git commands

Git difference between files

git diff <file1> <file2>

Move to master branch 
git checkout master  

Update your remote tracking branch git fetch Fetch compared with pull

git pull command performs a git fetch and git merge
git fetch does not perform any operations on your local branches.



You can always run the fetch command and review the incoming changes.

Merge command

Merge the branch with the current branch

git merge dev-testing


Cherry-pick
git cherry-pick

command allows you to select the patch which was introduced with an individual commit and apply this patch on another branch

command can be used to change the order of commits

Reset command

git reset --hard

command makes the working tree exactly match HEAD but it does not delete untracked files in your working tree

Rename / Alter the command

git commit –amend “commit message”

git blame
---> show the version and author modified each line of the file