Sunday, May 31, 2015

Just a blog : A quick guide to git and GitHub (Part 1)

There is plethora of git guides over internet, some are really good and some really suck. I here am writing a simple blog for the newbies who have successfully made an account over github and are really excited about it.

GitHub is a web-based Git repository hosting service, which offers all of the distributed revision control and source code management(SCM) functionality of Git as well as adding its own features. Unlike Git, which is strictly a command-line tool, GitHub provides a web-based graphical interface and desktop as well as mobile integration

Or simply

Github connects you with other people who are interested in sharing and creating together.

So there are really two ways to begin with. Here goes the first one.

Create a repository : Repository is what you call the bundle or stuff you are going to upload and work on. Go to https://github.com/new and create a repository. Let's name it hello-world.
Add some description, or you might want to add it later. Initialize it with a Readme. It's a good practice and let's you clone it immediately. (Yeah right, see below)

Clone it : Open your console, move to the directory where you want to work and do this.
 $ git clone https://github.com/username/hello-world.git
This will create a directory in your computer. Now, this isn't a simple directory. It is a 'clone' of the github repository. You can say that the files in it are controlled by the version control system git and tracked with the repository on github.

Do this,
 $ cd hello-world  
 $ git remote -v  
 origin    https://github.com/<username>/hello-world.git (fetch)  
 origin    https://github.com/<username>/hello-world.git (push)  
This shows that the link to repository of yours at github is known by the name origin over here.

Now another way of this is first you create the directory on your computer. Navigate inside it and do
 $ git init  
This will initialize an empty git repository inside it. Later, you can add the link to the repository by the git remote command like this
 $ git remote add origin https://github.com/<username>/hello-world  
You can always use git remote -v command to track the aliases.

Making the first change : Okay now we are in condition of adding some files or code to this repository. I am going to make a python file with hello world program and publish the change over the github repository too.
So make a new file say hello.py inside the directory, open it with your favorite editor and write the one line code to print whatever you want. Why just hello world? (Because dmr did it!) Now write it and save the file.

The first thing you should always do is to see whether you have done any changes or not in the repository. This can be done by
 $ git status  
After the changes you made, you must be getting this after git status
 On branch master   
  Your branch is up-to-date with 'origin/master'.   
  Untracked files:   
  (use "git add <file>..." to include in what will be committed)   
    hello.py   
  nothing added to commit but untracked files present (use "git add" to track)   
Add, Commit, Push! : In your console, do this.
 $ git add .  
This adds all the files in the track which you have changed.
Now it's time to commit that change. By committing, git understands that you want the change to be in record. Every commit is accompanied with a message that appears on the log (git log) Do this
 $ git commit -m "Added hello.py"  
You've made your first commit! But it won't be appearing over github, because you made the changes locally. You need to push it over the web. For that, you use
 $ git push  
Or,
 $ git push origin  
to say git where actually you are pushing it to. Or,
 $ git push origin master  
to say git over what branch on origin you are pushing it to.

That's it! You've published your changes to your GitHub repository.

NOTE : This is a very basic start. There are more concepts to be explored. You can search for specifics. StackOverflow has also great resource. This is one of those posts, which are not intended to bring the dark side of git to the face.

Happy Coding!

9 comments:

  1. Adding Branching and Merging techniques would make it a complete quick guide.

    ReplyDelete
  2. I was thinking of making a separate post for that. :) People can take a start from here.

    ReplyDelete
  3. Open the cmd windows console? Or is there a git console I need to download?

    ReplyDelete
    Replies
    1. Use git console. That's far better than command prompt.

      Delete
    2. ^ +1

      @Bethell I have used Git on both the cmd prompt and Git Windows Client. And the Git console is definitely the better choice.

      Delete
  4. I good thing will be to tell the common convention of when to use which branches. Many people often send PR's from master branch and then pull over then changes making the tree dirty and different from the "upstream" repo. Then a second PR's becomes way too absurd.

    ReplyDelete
    Replies
    1. Hey Pranit, thanks for looking into this. This post did not explain about creating a PR. There was a followed up post for this http://orkohunter.blogspot.in/2015/06/just-blog-quick-guide-to-git-and-github.html

      Delete
  5. Free easy & simple way to learn programming online we provide niit projects, assignments, cycle tests and much more..
    visit ====>>> https://githubhelp.blogspot.in

    ReplyDelete