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 typinggit branch -l any-branch-name
. Now rungit branch
again and you will see that there is your new branch now along with your master. You can delete a branch by typinggit 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 typinggit checkout branch-name
. Again check your working branch. You can also create branches with checkout command by typinggit 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 typegit log --name-status
. To see the movement of HEAD pointer you can view reference log by typinggit 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 typinggit 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...
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
Post a Comment
Comment on articles for more info.