Difference between revisions of "Basic git"

From Notes_Wiki
m
m
Line 8: Line 8:
     git config --global user.name "Saurabh Barjatiya"
     git config --global user.name "Saurabh Barjatiya"
     git config --global user.email "saurabh@sbarjatiya.in"
     git config --global user.email "saurabh@sbarjatiya.in"
     git config --global push.default simple
     git config --global push.default current
     git config --global color.ui true
     git config --global color.ui true
     git config --global color.status auto
     git config --global color.status auto

Revision as of 04:05, 8 November 2013

<yambe:breadcrumb>Git</yambe:breadcrumb>

Basic git

Git config

When using git for first time we should set global config values that can be used in all git projects using

    git config --global user.name "Saurabh Barjatiya"
    git config --global user.email "saurabh@sbarjatiya.in"
    git config --global push.default current
    git config --global color.ui true
    git config --global color.status auto
    git config --global color.branch auto 
    git config --global core.editor vim 

The above commands mostly end up creating a file '~/.gitconfig' which includes:

[user]
        name = Saurabh Barjatiya
        email = saurabh@sbarjatiya.in


We can use

    git config user.name "Saurabh Barjatiya"
    git config user.email "saurabh@sbarjatiya.in"

to configure a project specific name and email address. This configuration goes in .git/config file of the project


Initializing a new repository

To initialize a directory as working copy of repository we can use command:

git init

when current working directory is parent most directory of folder which we want to initialize as git repository.


Committing changes

To commit changes for every new / modified file that we want to go as part of commit we need to run:

git add <filename>

Note that this is different from most centralized version control systems where we do add only once and future commits automatically check-in modifications of the file. In git we can modify a file and choose not to include modifications in commit by not using git add on modified file. Once all the files that are intended to be committed are added we can use:

git commit

to commit the changes.


If we want to add all the modified files which have been committed in some earlier version for committing then we can use:

git add -u

which automatically adds all the modified files (not the new files). We can combine commands 'git add -u; git commit ' to single ' git commit -a' command which does both adding of modified files to index and commiting without adding new files.


Status of changes

We can use:

git status

which lists three different types of files: 1. Files that were modified and added to index. These modifications will become part of next commit. 2. Files that were modified but not added. These modification will not become part of commit if performed now before making any other changes. 3. Files that are not being traced by git.


Seeing commit logs

We can use:

git log

command to see log of commits to current repository. If we are interested only in commit logs of a specific file then we can use

git log <file_name>

We can also see how many lines were changed due to each commit using --stat option as

git log --stat <filename>


Seeing differences

We can use git diff to get differences since last commit, difference wrt some branch or some tag using :

git diff  [  |<branch_name> | <tag_name> | HEAD ]

or

git diff --stat

command to see differences since last commit. We can use git diff to see difference between current files and some other branch etc.


Tagging current commit

Adding tags

We can tag current commit with help of

git tag <tagname>


Listing tags

We can list tags matching given pattern with

git tag -l <pattern>

If pattern is not specified then all tags are listed.


Deleting tags

We can delete tag using

git tag -d <tagname>


Specifically ignoring files

We can ask git to specifically ignore files by mentioned their names in '.gitignore' file. This ignore file can understand patterns and applies to all sub-folders and files. We can create .gitignore in any folder and then it will apply to its subsequent sub-folders and files.

Sample .gitignore may have lines:

# Lines starting with '#' are considered comments.
# Ignore any file named foo.txt.
foo.txt
# Ignore (generated) html files,
*.html
# except foo.html which is maintained by hand.
!foo.html
# Ignore objects and archives.
*.[oa]


Restoring file from previous commit

We can restore file to earlier state using:

  1. First use 'git log <file>' to get different commits made to file
  2. Choose the commit to which you want to restore. You can use 'git diff <commit> <file>' to diff file at time of given commit to current file
  3. Reset the file to given commit state as per index using 'git reset <commit> <file>'
  4. Restore older version of file from index using 'git checkout <file>'


A good tutorial on git is located at http://www.vogella.com/articles/Git/article.html



<yambe:breadcrumb>Git</yambe:breadcrumb>