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

Search box for blog or website

Image Search Engine Using Python

Cordova viewport problem solved