Sunday 28 February 2016

Git Basics and Commands

git logo
Today, Lets discuss about the version control of software & how a source code version control system can help you to maintain your software flawlessly. There are many version control systems are present in the market right now but as per my opinion & experience "Git" is the best solution for version control. Git is a Free & Open Source, Distributed software version control system. It can handle a wide range of software products ranging from very small to extra large projects. The efficiency & speed you get with Git is the best. Also, Git ensures freedom to share your projects under the "GNU General Public License Version 2.0".

In this post I am giving you the Git commands & its one line explanation. You could try all these commands to make full use of  Git. Wish you Happy & tension free Source Control Management (SCM).

You can download Git from the official website "git-scm"

A. The First Step is to configure user information for your local repositories.

1. Set the name you want to use for your commit transactions.

$ git config --global "[name]"

2. Set the email you want to use for you commit transactions.

$ git config --global "[email address]"

3. Enable helpful colorization of command line output.

$ git config --global color.ui auto

B. Create Repository

1. Start a new git repository.

$ git init "[project-name]"

2. Download a existing project and its entire version history from a URL/website link.

$ git clone "[URL/web-link]"

C. Make Changes to your Project

1. List all new or modified files.

$ git status

2. Check the file differences between files.

$ git diff

3. Add a new file into the project to be maintained like existing files.

$ git add "[File Name]"

4. Check the difference between modified file & last file version

$ git diff --staged

5. Remove file from staging but preserve its modifications.

$ git reset "[File Name]"

6. Save modifications permanently.

$ git commit -m "[Description Message about this commit]"

C. Group Changes

1. List all local branches in current repository. Helpful if you are working in multiple sections at a time.

$ git branch

2. Create a New branch

$ git branch "[Branch-Name]"

3. Switch to specified branch & update the working folder/directory location. You need to commit before you switch the branch. You may save or delete your changes prior to switching the branch.

$ git checkout "[Branch-Name]"

4. Merge/ combine the specified branch history into current branch

$ git merge "[Branch-Name]"

5. Delete specified branch

$ git branch -d "[Branch-Name]"

D. Review History

1. List version history of the current branch

$ git log

2. List version history for a file including renames

$ git log --follow "[File]"

3. Display current difference between two branches

git diff "[First-Branch]" "[second-Branch]"

4. Display Metadata information & content changes of the specified commit

$ git show "[commit]"

E. Redo/Reset Commits

1. Undoes all commits after "[commit]" & preserving changes locally.

$ git reset [commit]

2. Discard all history & changes back to the specified commit

$ git reset --hard "[commit]"

F. Save Changes

1. Temporarily stores all modified tracked files

$ git stash

2. Restores the most recently stashed files

$ git stash pop

3. List all stash changes until now

$ git stash list

4. Discard the most recently stashed changes

$ git stash drop

G. Synchronize Your Changes

1. Downloads all history from the repository.

$ git fetch "[bookmark]"

2. Combine bookmarks branch into current local branch

$ git merge [bookmark]/[branch]

3. Uploads all local branch commits to GitHub

$ git push "[alias]" "[Branch]"

4. Downloads bookmark history & incorporates changes. Use this if you are working in a team project where all people are working on the same branch & updating it time by time.

$ git pull

H. Refactor File names

1. Delete the file from working directory & stages the deletion

$ git rm "[File]"

2. Remove the file from version control but preserves the file locally

$ git rm --cached "[File]"

3. Change the file-name & prepare it for commit.

$ git mv [File-Original] [File-Renamed]

Now, after all this important information if you want to suppress tracking of some files temporary or accidentally then use following commands. First for list all ignored files in this project. Second is a text file named ".gitignore" which suppresses accidental versioning of files & paths matching the specified patterns.

$ git ls-files --other --ignored --exclude-standard


Previous Post : How to install cuda on Ubuntu 140.4

Next Post :  Open Source Home Page

==> Posted By Yogesh B. Desai

No comments:

Post a Comment