Git tutorials



To install git

sudo apt-get update
sudo apt-get git-core

To check the version of git
 git --version


To setup Git configuration

1. Git initialize :-
   git init
2. check for git status :-
   git status
3. Add user name :
   git config --global user.name "email@guidanz.com"
4. Add email id for user :-
   git config --global user.email email@guidanz.com
6. Add git  repository :-
   git remote add origin https://github.com/try-git/try_git.git
7. Clone repository :-
   git clone  https://github.com/try-git/try_git.git

Set proxy settings in git
 git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
 git config --global https.proxy https://proxyuser:proxypwd@proxy.server.com:8080

Remove proxy
 git config --global --unset http.proxy
 git config --global --unset https.proxy

Create a new project

Assume that we have to create new project under git revision control.

Create a new directory called myproject
$ mkdir myproject
$ cd myproject

To initialise git
 $ git init

Git will reply as
 Initialized empty Git repository in .git/

Create new file called test.txt and add some contents like helloworld
sudo echo helloworld >> test.txt

To store in a temporary staging area
 $ git add .

To permanently store the  contents of the index in repository
$ git commit -m "message"

Note :- you mandatory give  message
This snapshot is now stored in a temporary staging area which Git calls the "index". You can permanently store the contents of the index in the repository with git commit:

$ git commit

This will prompt you for a commit message. You’ve now stored the first version of your project in Git.
Making changes

Modify some files, then add their updated contents to the index:


$ git add file1 file2 file3

You are now ready to commit. You can see what is about to be committed using git diff with the --cached option:

$ git diff --cached


Viewing project history

At any point you can view the history of your changes using

$ git log

If you also want to see complete diffs at each step, use

$ git log -p

Often the overview of the change is useful to get a feel of each step

$ git log --stat --summary

Managing branches

A single Git repository can maintain multiple branches of development. To create a new branch named "experimental", use

$ git branch experimental

If you now run

$ git branch -a

you’ll get a list of all existing branches:

  experimental
* master

The "experimental" branch is the one you just created, and the "master" branch is a default branch that was created for you automatically. The asterisk marks the branch you are currently on; type

$ git checkout experimental

to switch to the experimental branch. Now edit a file, commit the change, and switch back to the master branch:

(edit file)
$ git commit -a
$ git checkout master

Check that the change you made is no longer visible, since it was made on the experimental branch and you’re back on the master branch.

You can make a different change on the master branch:

(edit file)
$ git commit -a

at this point the two branches have diverged, with different changes made in each. To merge the changes made in experimental into master, run

$ git merge experimental

If the changes don’t conflict, you’re done. If there are conflicts, markers will be left in the problematic files showing the conflict;

$ git diff

will show this. Once you’ve edited the files to resolve the conflicts,

$ git commit -a

Using Git for collaboration

Suppose that Alice has started a new project with a Git repository in /home/alice/project, and that Bob, who has a home directory on the same machine, wants to contribute.

Bob begins with:

bob$ git clone /home/alice/project myrepo

This creates a new directory "myrepo" containing a clone of Alice’s repository. The clone is on an equal footing with the original project, possessing its own copy of the original project’s history.

Bob then makes some changes and commits them:

(edit files)
bob$ git commit -a
(repeat as necessary)

When he’s ready, he tells Alice to pull changes from the repository at /home/bob/myrepo. She does this with:

alice$ cd /home/alice/project
alice$ git pull /home/bob/myrepo master


Undo the last commit

git reset --hard HEAD~1


Comments

Popular posts from this blog

Proxy setting in java

Using logstash to import csv json files into elasticsearch

Kibana 4 Installation and Run as a service in ubuntu