Git and Github on a single page

git
6 minutes read

GIT

Git  is used for version controlling of any type of file (does not works well with binary files). This is most popular in software development scenarios. People can work in team with this type of distributed code maintenance service. This software can be easily installed on linux based system, therefore has a good command line interface with many commands.

We will discuss about most common commands used to maintain the repository.

Therefore this post can be used as a handbook while working with GIT on your local or remote repositories.

Note:- I assume that you are using a linux machine with Debian distribution.

Useful Commands

To install the git

sudo apt-get install git

Initialize git in current directory

git init

Check Status

git status     --> Displays the current state of the repository
git status -s  --> Displays the output in short format
git status -b  --> Displays the branch and tracking info in short format
git status -u  --> Displays tracked files

Add

This will add file to the staging area before commit.

git add <path to file_1> <path to file_2> ...... --> add any number of files.

git  add <directory>                             --> adds full directory.

git add .                                        --> adds files and directories in current directory.

git add *.txt                                    --> adds all the files with .txt prefix (wildcard used).

git add -f <filename>                            --> add files even if it is included in the .gitignore file.

git add -u <filename>                            --> adds updated file which was previously staged.

Commit

Stores the files in the current stage.

git commit              --> saves the file and open the text editor for message(metadata).
git commit -m "message" --> saved the file with your entered message.
git commit -a           --> stages and commits all the files. Does not effects un-tracked files.
git commit --amend      --> this is used if you want to add files to an earlier commit.

Reset and Checkout

To understand this better we have to understand the working of git, looking a little into the design of GIT.

There are three parts in a git.

  1. HEAD – This is pointer which points to the previous commit block.
  2. Index  – This is pointer which points to the next commit of the present block. However, this block has not been committed. This can also be known as the staging area.
  3. Working Directory – This is where you are making changes to your files.

Now let us look at the cheat table below for quick reference and how the above commands will work.

HEAD Index Workdir WD Safe?

Commit Level

reset --soft [commit]

REF

NO

NO

YES

reset [commit]

REF

YES

NO

YES

reset --hard [commit]

REF

YES

YES

NO

checkout <commit>

HEAD

YES

YES

YES

File Level

reset [commit] <paths>

NO

YES

NO

YES

checkout [commit] <paths>

NO

YES

YES

NO

Note:- “checkout” is similar to “reset –hard” as it changes all the three things HEAD, Index and Working Directory. However, difference is that changes for checkout the changes are recoverable in contrast to reset –hard. Additionally checkout can be used to switch between the branches (eg-: checkout <branch name>).

This article has been written by taking the reference from the below article. For better understanding please refer the following link.

https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified

Diff

This will show the differences from the last commit to the present modified file.

Note-: The will not show any changes from the Working Directory to Staging area.

git diff

To get the difference from the staged area and last commit, enter the following command.

git diff --staged

Branching and Merging

Branch

To display all the branches of the present repository

git branch

Create a branch with specific name

git branch <name>

To create a new branch and move the header to the current branch

git checkout branch <name>

To delete a branch if it has not been merged.

git branch -d <name>

To delete a branch forcefully, even if the branch is merged or not.

git branch -D <name>

List remote branches

git branch -r

List all the branched which have been merged

git branch --merged

List all the branches which have not been merged

git branch --no-merged

Checkout

Switch to an specific branch with its name

git checkout <branchname>

To make changes in the files of an earlier commit, we use the following command. This will bring that committed files in the working directory to proceed for changes.

git checkout <commit>

We can also revert back to an earlier commit and bring in the staged filed from an earlier commit. By using this we can add files or folders of an earlier commit to the the present commit.

git checkout <branch>~[n]

where n=3 indicates 3 commits back. Example “git checkout master~[3]” – this means 3 commits back from master branch.

Merge

This will merge other branches to the current branch. We can also merge multiple branches to the current branch by giving there names with a space in between.

git merge <branchname_1> <branchname_2> <branchname_3> .......

To merge the branch without committing.

git merge --no-commit <branchname>

Note:- There may be places where conflicts may arise (if we have made changes on same line in two different branches). We have to resolve this manually.

An example of conflict is as follows.

Here are lines that are either unchanged from the common
ancestor, or cleanly resolved because only one side changed.
<<<<<<< yours:sample.txt
Conflict resolution is hard;
let's go shopping.
=======
Git makes conflict resolution easy.
>>>>>>> theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.

Where the two sides are differentiated by ===== , <<<<<< means one side and >>>>>> is the other side.

To resolve this you can launch a graphical user interface by giving the following command. Delete everything which is irrelevant to you and keep only the code you need.

git mergetool

Log

Show all the commit history

git log

Show commit history, however ignore merges

git log --no-merges

Show commit history using Hashes and short messages

git log --pretty=online

Show all the commits that has effected a specific file

git log <file>

Reverting & Resetting

Checkout

Takes you to an earlier commit. You can make changes and commit the file again.

git checkout <commit>
We can also revert back to an earlier commit and bring in the staged filed from an earlier commit. By using this we can add files or folders of an earlier commit to the the present commit.

git checkout <branch>~[n]

where n=3 indicates 3 commits back. Example “git checkout master~[3]” – this means 3 commits back from master branch.

Rebase

Arranges commit history into a linear sequence. Moves the current branch to the specified commit. This is mostly done after a merge which moves the latest commit at last.

git rebase <commit>

Working with remote Repositories

Remote

This is where we manage remote repositories. To list all the tracked remote repos.

git remote

To add new remote repo. The name establishes an alias for the repo, while the URL establishes the location.

git remote add [name] [URL]

To remove a specified remote repo with its alias.

git remote -rm [name]

Fetch

By this we fetch the remote directory onto our local directory. This is not automatically merged with the local repository. Therefore you have to inspect and merge the repo manually.

The following command will fetch from remote repository tagged to current branch.

git fetch

To store all the branches from remote repository and store them locally.

git fetch origin

Pull

This will pull from remote repository and sync with your local repository. In fetch you have to merge manually, which is not in this case.

To pull the current remote repository and merge it.

git pull

To pull the specified branch from remote repository and merge it.

git pull origin <branchname>

Clone

To copy the remote repository on to your local git repository, we use this command. This copied the whole repository as it is in the remote server.

The following command will get the repository form the specified URL.

git clone [URL]

To clone the remote repository and rename it locally.

git clone -o [URL] <name>

To clone the remote repository and checkout into a specified branch.

git clone -b [URL] <branch>

Push

This is the reverse of pull. It will update the remote repository with local repository.

To push the current branch to the remote branch.

git push

To push the specified version of the branch to the remote’s  version of that branch.

git push [remote] [branch]

Delete a specified branch on the remote directory.

git push [remote] :[branch]

 

 

Thank you, I hope this will serve as a quick guide while working with GIT. If you are using Github on a linux machine, this will be the best guide for you as Github Desktop client is not made for Linux.

Leave a Reply