Pages

Showing posts with label github tutorial. Show all posts
Showing posts with label github tutorial. Show all posts

Git and GitHub Tutorial

What is a version control and usage?

When we write a project with a number of files, it is not easy to maintain. Some times, we might have edited the source file a lot but later we might realize that we want the earlier version of the source code. Git does that.

Git is a version control system which can be used to manage changes made to the files and directories in any project. There are a number of version control systems out there. But, git is famous may be because of its developer (who?).

Anything saved to git is ever lost and at any time you can go back to earlier versions and also can clearly see which version of the program generated which version of the output.

It is also useful when you work with collaborators. Git will notify the changes as well as confilicts with others. So, overwriting problem will not occur.

"Git can synchronize work done by different people on different machines, so it scales as your team does."

Version control uses: It is not just for developing code. It can be used for writing books, books, journal papers, parameter sets (anything that change over time or need to be shared)

The first step:

Create a directory (My-Repo) in your home directory or anywhere in your linux system.

Now type, git status in that directory.

Now you will get.
fatal: Not a git repository (or any of the parent directories): .git
This shows that, the current directory is not a git repository. To start that directory as a repository, you have to use

git init

Now,

Initialized empty Git repository in /home/user/path/to/directory/My_Repo/.git/

will be displayed. This means, the current directory has been initalized as Git repository.

git status

On branch master

Initial commit


nothing to commit (create/copy files and use "git add" to track)


This means that we are in the master branch of the repository.

Now, create an empty file inside this directory (i.e., repository). You can use touch hello.txt

Now, again type

git status

Now, you will get,

On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        hello.txt

nothing added to commit but untracked files present (use "git add" to track)

This means that (git says) a new file hello.txt is created in the repository (now the current directory is a repository) and the file is not tracked by the git.

To make the git to track the created file hello.txt, you have to use the command

The git has following status of the files.

untracked: Git is not tracking this file yet, usually indicating that the file is new.
tracked: The file is part of the Git snapshot and Git is tracking changes to it.
unstaged: A tracked file has changes but it has not been staged for commit.
staged: The file is ready to be committed.
deleted: The file has been deleted and should be removed from Git.

Now, the steps to follow is
git add hello.txt

This upgrade the status of the hello.txt to staged.

Now, if you type

git status in the repository,

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   hello.txt

will be displayed. This means that the file hello.txt is tracked.

We can more files and just use git add (fileName). This will add all the files to staged mode. Or you can use
git add .
This command will all the files in the current directory to track mode.

So far, your file is just staged. To make it track,

git commit -m "This is a new repository"

The message inside the "" is for reference purpose.

Now, you will get following message.

git commit -m "This is to learn git and github"
[master (root-commit) 8de3ed7] This is to learn git and github
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt
 create mode 100644 hello2.txt

Now, at this stage, don't worry about connecting to GitHub and configuring your email ID to github etc. We will see them later.


Now, we will see little bit about the git config.

To see the status of your configure,

cat .git/config

This will display the configure name and emailID.

You can also try
git config --global --list

This will display the name and email associated with the repository.

If not name and emailID is not set so far, follow this.

git config --global user.name "yourName"
git config --global user.email "yourid@gmail.com"

Now, if you hit
git config --global --list

you will get

YouName
user.email=yourid@gmail.com
user.name=yourName

The basic workflow is as follow:

git status
git add
git commit
git push

If your configuration correct, your changes would be pushed to the remote repository.
Till git commit, you can work locally and saving the track of each modification by using add and commit. But, if you want to export the changes to the remote repository, you have to use git push.

In my case, first time I am trying to push to the remote repository.

git push

warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Now, I am prompted to enter the username and password:

Username for 'https://github.com': userName
Password for 'https://userName@github.com':
To https://github.com/userName/github-workflow.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ssthurai/github-workflow.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

This shows that the push is not success. Let us see more about this push later. Keep in touch.

When we commit a change in a file, we are prompted for inserting comment. For me, the default browser "nano" came. However, I prefer 'vim'. To change to vim as a default editor

git config --global core.editor "vim"









You may be interested in these posts

Error in image file conversion: convert-im6.q16: not authorized `test.eps' @ error/constitute.c/WriteImage/1037.

This error is because of the vulnerability. This allows remote execution of code using image formats. So, some Linux distributions by defaul...