Git Talk

It is no surprise that Git took the world of source control management by storm since its inception in 2005. For people wondering about what I am talking here, Let me give some context first. Generally, Any reasonably sized source code project needs some sort of ‘change tracking software’ about all the changes/modifications made to it by its authors/programmers. This is where the concept of Source Control Management(SCM) comes in to picture. The idea is nothing new at all. Git is just one such SCM but with some radical ideas built from the grounds up. Ofcourse, These ideas sets it apart from its predecessors.
Read on →

How to Do Interactive Rebase in Git?

Today I discovered yet again a new gem in git. Think of the following scenario. You have  a bunch of 7 commits on your branch and you want to make some modifications to the intermediate commit. This is not uncommon and is a real pain to redo all your work just to modify one intermediate commit. Git provides a unique way to solve this problem using –interactive to git rebase command.

Currently I am on branch Aug23_TOT and I want to modify 4th commit before pushing all my 7 commits.

Current branch with above said 7 commits
Current branch with above said 7 commits

Commit to be modified

Commit to be modified
Commit to be modified
Interactive rebasing of last 7 commits
Interactive rebasing of last 7 commits( git rebase -i )

Opens up the editor with the last 7 commits listed and with the default action specified before them.

Rebase editor to choose from commits to modify
Rebase editor to choose from commits to modify

Make necessary changes to action from the help description below and save and quit the file.

Change 'pick' to 'edit' to tell git which commit need to be edited
Change ‘pick’ to ‘edit’ to tell git which commit need to be edited

Immediately git rebases first 3 commits as they are specified as ‘picks’ and it stops right next after the first 3 commits and wait for us to make required modifications.

Git stops after rebasing first 3 commits
Git stops after rebasing first 3 commits

You can make any changes(modify files) to the commit, I am just editing the commit message here. After that, appropriately save your changes back to git commit history using usual git add, git commit commands

Here in 4th commit I am updating the commit message
Here in 4th commit I am updating the commit message

After making required changes enter git rebase –continue

Tell git to continue further with remaining commits
Tell git to continue further with remaining commits
Final changes
Final changes

Without this interactive rebase feature the task of changing an intermediate commit would be very tedious and time consuming.

Git revisited

  1. git add -u
    • Add all modified files into staging area.
  2. git commit -a
    • Automatically adds all tracked files  to staging area for committing
  3. git diff –staged
    • Diff the content already added to staging area( using git add command )
  4. git remote add <alias> <remote git repo location>
    • Makes a reference to the remote repository with the given name
    • Example: git remote add origin https://github.com/vineel567/project123.git
    • orgin acts as an alias to the remote repository
  5. git remote -v
    • List all remote repositories aliased
    • Example: git remote -v
    • origin  https://github.com/vineel567/project123.git (fetch)
    • origin  https://github.com/vineel567/project123.git (push)
  6. git fetch <remote alias>
    • Fetches the remote repo to local but does not merge on to the current branch, Instead creates a new remote tracking branch
    • git br -a  will show all the remote branches in red color
      • * master
        remotes/origin/master
    • git remote show <remote alias> will show the info of all the remote and local branches with respect to the remote repo.
      • * remote origin
        Fetch URL: https://github.com/vineel567/project123.git
        Push URL: https://github.com/vineel567/project123.git
        HEAD branch: master
        Remote branch:
        master                tracked
        Local ref configured for ‘git push’:
        master pushes to master (local out of date)
  7. git pull <remote alias> <remote branch>
    • Fetches the remote repo to local and also merges onto the current branch, Also creates a new remote tracking branch
    • git br -a  will show all the remote branches in red color
      • * master
      • remotes/origin/master
    • git log will show the detailed commits recreated after the pull
  8. git push <remote alias> <branch>
    • Find a ref that matches <branch> in the source repository (most likely, it would find refs/heads/master), and update the same ref (e.g. refs/heads/master) in <remote alias> repository with it. If <branch> did not exist remotely, it would be created.

References: http://gitref.org/