Skip to main content

Learn Git & Github - 3

Git Commands: Lesson-3

What we learned in Previous Lesson

In the previous lesson, we learned about github and how can we use it along with git to create an open source project(A project which is free to use and distribute). We set up our project on github and cloned it in our machine to modify it locally(On our machine only). Now either you have cloned fork of an existing project or your own project, the development process is nearly the same. I'll point out the difference wherever applicable.

Git Terminology

In first lesson, we talked about 3 most occuring terms of git which are branch, commit and head. Now Lets talk about some more terms which are associated with git

  • Index :Index is the current state of the project for git. If you have changed some files then you will have to add them to git index so that git can track those files for future changes. If you don't add some file to index then it won't be tracked. Also it is necessary to add files to index to make commits to those files.
  • Push :Pushing means sending the changes we made on our machine in our project to the hosting service or remote repository which is our github repository. So what does it actually mean that if we changed some files of our project and we want to upload the changes to github then it is called pushing to the remote.
  • Pull :Pulling means getting the current state of the remote branch. There can be lots of branches in the github project. If a team is working on a project and some developers have brought changes to some files of a branch then you can update your local project(Project on your machine) by pulling those changes. So if you have forked an existing project then what you do to keep your branches updated is you pull the changes from the original project branch to your local project branch then you push those changes to your github forked project.
  • Rebase : Rebasing means putting your commits over the main project commits. For example, if you are editing some files and make some commits but before you push them to your github project some team member has updated original github project then what you would want will be to put your changes on the top to prevent conflicts between your changes and other changes which that team member brought. So in this case we pull that developers's changes with a rebase which will update our local project with the main project and then put our local commits on top of commit history. Now when you will push your changes then your changes will be the most recent ones.

There can be some other terms also which you may notice while searching for a answer but those terms are actually the command names which we'll learn in the next section.

Git Commands

Git is actually a command line software which means you will type commands in terminal or git-bash to tell what to do to git. So to use git, you must be familiar with many of its commands. In this section, i'll tell you some git commands with which we'll continue our project development. If you have set up your project on your machine then you are ready to go but if you have not then i'll advise you to go to previous lesson and set it up first.Also make sure that your present working directory is your project directory otherwise running the following commands will throw error. Also i recommend you to fork and clone fetch-info-github repository so that you can make sure that nothing gets messed up and i can visualise what you are seeing right now.

Before we start, lets set our usename and email on git by typing git config --global user.name your-name and git config --global user.email your-email

  • branch :We have cloned the project. So first lets see what are branches do we have initially. The command git branch will list the branches which are present in out working tree. Currently there should be only master branch. So lets create one for development purposes. You should not change the master branch directly because you can have it as reference point from where to continue the work. Always create a new branch for some further development. Make commits to that branch. If everything works fine then merge that branch with master and push it or push it to some other branch on github and merge it there with master and pull the remote master to your local master. But for now, lets just create a branch by typing git branch -l any-branch-name. Now run git branch again and you will see that there is your new branch now along with your master. You can delete a branch by typing git branch -d branch-name
  • checkout :Checkout is used to navigate your git tree. It can help you switch your working branch, create a new branch and move between commits. Here we'll limit ourself to only switching and creating branch. Moving between commits and doing stuff there will be covered in next lesson. First check your working branch by git branch. Working branch will be colored. Now switch to some other branch by typing git checkout branch-name. Again check your working branch. You can also create branches with checkout command by typing git checkout -b new-branch-name. So far so good.
  • log and reflog :Log and Reflog commands are used to see the git history. Type git log and it will show the commits made so far along with their authors and time stamp. If you want to see associated file names then type git log --name-status. To see the movement of HEAD pointer you can view reference log by typing git reflog
  • status :Status command tells you the current status whether there are some untracked files(those files which haven't been added to git index) or any merge conflicts. To check status type git status
  • add :Now lets start some real stuff. Create a branch named "test" by checkout command. Check your working branch and it must be "test". Now create any file you want or open any file you want to modify. Change some code or write some code in new file and save the file. Now check status of your project and this time you will see some red lines telling you that your modified file or created file hasn't been added to tracked list. To add that file type git add "file name". You can also add the whole directory to index by typing git add .. Now again check the status. It will show that files have been added to index but changes haven't been commited yet.
  • commit :Now lets commit our changes. Type git commit -m any-message-or-change-description. NOw check status again and it will show that everything is fine. Check your log by log command and you'll see that your commit is on the top of history. The first 7 characters of colored line in git log is the commit id. For example, if you see something like commit 18b67246fa50778bd64fe0462bce45357fc5abd5 then your commit id is 18b6724.
  • pull :Okay so we have made our first commit. But we are not sure whether we have the updated version of the branch or not. To update the project, we will pull the remote project changes by typing git pull --rebase origin master This way our project will be synced with the remote project and our commits will be on top if there are no merge conflicts. Merge conflicts resolution will be covered in later lesson.
  • push : Now lets push our commits to the remote project. In your case, your forked project. We do so by typing git push origin branch-name. Then you'll be prompted with github username and password then it will upload your changes to desired branch.

Finally we have made some commits and have pushed them to remote project. Now if you have created a new project then your changes will be in your branch and you can merge them with master directly on github. But if have a forked project then the changes will be uploaded to the branch of your forked project. To let the developers of original project know about your changes and to request them to add those changes on their original project, you'll have to create a pull request directly on github. All these minute details will be covered in next lesson and we'll also learn to navigate git tree so that we can precisely control our project versions. So stay with us. You can subscribe to our newsletter just by entering your e-mail address and you'll get notified about the next lesson by your e-mail directly. So what are you waiting for..Sign up Now...


Enter your email address:


Please don't forget to respond to confirmation mail sent to you for this subscription. If you don't click the link sent to you, you won't be subscribed.

If you have any doubt or need any help then just a comment below.


Comments

Popular posts from this blog

Image Search Engine Using Python

Images provide a lot more information than audio or text. Image processing is the prime field of research for robotics as well as search engines. In this article we will explore the concept of finding similarity between digital images using python. Then we will use our program to find top 10 search results inside a dataset of images for a given picture. It won't be as good as google's search engine because of the technique we will be using to find similarity between images. But what we are going to make will be pretty cool. So lets start. Setting up the Environment Our Algorithm How the code looks Lets build the GUI Additional Techniques Setting up the Environment The code we are going to write requires a few tools which we need to install first. I will try to be as precise as i can and if you get stuck into installing some tool then you can drop a comment below and i will help you sort out the problem. So here are the tools and the steps to install

Understanding Python Decorators

If you have ever wondered what those @something mean above a python function or method then you are going to have your answers now. This @something line of code is actually called a decorator. I have red from various articles about them but some of them were not able to clarify the concept of a decorator and what we can achieve with them. So in this post we'll learn a lot about python decorators. Here is a list of topics we'll be covering. What is python decorator Understanding the concept Multiple decorators on same function class method decorator Where can we use decorators What is python decorator A python decorator is nothing but a function which accepts your given function as a parameter and returns a replacement function. So its like something this def decorator(your_func): def replacement(your_func_args): #do some other work return replacement @decorator your_func(your_func_args): #your_func code Now when your_func gets called then

Cordova viewport problem solved

Include the viewport settings in Cordova If you are facing the auto zooming problem of cordova then go read on the full article. Cordova actually ignores the viewport meta tag which causes the pixel density problem. So we need to tell cordova that viewport tag is equally important as other tags. To do this, we need to add some code to a file which is specify in the article. Corodva messes with pixels If you are using the latest cordova version or creating the cordova app for latest android versions then you may have faced the zoom malfunctioning.I also faced it when creating an app. Many of you may have already searched the web and found the answer of changing the meta tag attributes to get it working. But adding target-densitydpi=medium-dpi does not solve the problem for latest android versions. It may work for gingerbread but not for kitkat and others. So the final solution which i found was one of the stackexchange answer but rarely found. So i am gonna two things here, i