Skip to main content

Learn Git & Github - 4

Git Tree Navigation And Management: Lesson-4

Changing Remote Repository

In the previous lesson, we learned how to make commits. If totally followed the lesson then i am sure you world have made some commits also. Now change your working directory to your project directory. Check you are on master and Use git pull origin master to update your master. Since Now, we were syncing our local repository with our forked repository. But what we need is to keep updated with the original project repository. So we need to add the url of original project as our main remote repository. We can do so by typing git remote add main https://github.com/kapilgarg1996/fetch-info-github.git. If you have forked another project then just add its clone url. Now type git remote -v and you will notice that there are 2 remote repositories. One is origin which is your forked repo and another is main which is original project repo. You have control only over forked repo so you can "push" your changes to "origin" only. To update your forked with original project , you need to perform these 2 steps. Perform these steps when you are on your master branch

  1. Pull from main repo by git pull main master
  2. Push to your origin repo by git push origin master

This way, your local project and forked project will be in sync with the original project.

Commits Navigation

There are times when you make many commits and then realise that somewhere back you made some wrong changes. Then you can go to that commit and edit that commit. Or If you want to start a branch from a specific previous commit, you can go to that commit and start a branch from there.I told you in previous lesson, how to find a commit id. But if you dont recall then go to previous lesson and read about commit.

Jump to a specific commit

Before doing anything, create a test branch so that you can harmlessly run these commands. So i am assumming now that you have the id of the commit to which you want to go to. Now just type git rebase -i commit-id. This will bring you to that commit. It will open up an editor showing you the commits made after your given commit.Now lets what can we do in that editor.

Creating a branch from that commit

To create a branch from that commit, replace the word "pick" before that commit with "edit". Not press Ctrl + X and it will prompt to exit. Type "y" and press "Enter". Now the HEAD is at the commit id you supplied. Type git branch branch-name to create branch. Now to bring HEAD back to latest commit or to continue the work flow, type git rebase --continue.

Editing a Commit

Now lets see how to edit a specific commit.Editing a commit means editing the changes that commit introduced. To do so, just rebase to that commit id and replace "pick" with "edit" in the editor. Exit the editor and Now change whatever you want to change. Now you can either make multiples commits there or you can ammend that commit only by typing git commit --amend. Ammending a commit will keep the same commit but new changes you put.Now again continue your work flow by git rebase --continue

Merging 2 commits on 1

This is sometimes the case when you have created so many commits and want to merge some because some commits reflect only a change of a line or two. Lets see how to merge commits. Go to the specific commit and then the editor will open. The first commit in the editor will be your given commit. To merge the commits, replace "pick" with "sqaush" of the commits which you want to merge. Those commits will be merged with the previous commit whose command is "pick". Then exit the editor but this time it will ask you to edit commit message if you want. Then continue your workflow and check your git log. You will see that the commits have been merged.

Removing/Deleting Commits

This feature reflects the real power of git. Git is made so that you can control what changes you need and what you don't. Also, reverting the changes should be simpler than a text editor. In git we can remove commits and can undo the changes that commit made so far. Let me tell you how to delete specific commit or whole commit range in git.Suppose you have commit history like:

  • commit id34
  • commit id33
  • commit id32
  • commit id31
  • commit id30
  • commit id29

And you want to delete commits id31, id32. Lets suppose your branch is "branch1". Now to delete those commits type git rebase --onto id30 id32 branch1. Understand this code carefully because putting wrong commit ids can delete some important commits.

Pick/Move specific commit

Lets suppose that you have a fully working branch. Now you create a test branch to add some features. You made some important commits and then some not-so-important commits. Now if you want to add 1 or 2 specific commits of test branch on your master branch then there are two options. One is moving the commits to the master branch and second option is picking the commits from test and placing them on master. Lets have a look on both of them

  1. Pick commits :You can use cherry-pick command. Just type git cherry-pick commit-id. It will place the commit specified on the top of your current working branch.
  2. Moving Commits :You can move commits but before moving you must make sure that in the range of the commits, the last commit must be on top. For example, for this commit history :
    • commit id34
    • commit id33
    • commit id32
    • commit id31
    • commit id30
    • commit id29
    If you want to move commits id31 and id32 then go to commit id32 and create a branch from there. Lets say the branch name is "temp". Now temp will look like:
    • commit id32
    • commit id31
    • commit id30
    • commit id29
    Now you can move id31 and id32 to master by typing git rebase --onto master id31 temp

This is all for this lesson and for this guide. Whatever you have learned in these four lessons is sufficient to handle git and github.If you come across any problem then you can dig into stackoverflow or drop a comment below. If you have come across a problem called "merge conflict" then i would recommend you to visit This to see how to resolve merge conflicts. Also, i remind you to subscribe you to our newsletter which will update you about future posts directly by e-mail. So signup now..


Enter your email address:


If you face any problem then feel free to drop a comment below and i'll tell you a loophole.


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