Git advance tutorial – a complete guide

git advance tutorial

If you are reading git advance tutorial so I assume you already have been started working with Git. If not, then first go through my another article “Basic git commands with examples

Let’s go through the advance git command list which we would learn in this article.

Git push command – push your commits to remote repository

While you are working on your local repository, some other programmer might push some changes in the master which are related to a task you are working upon.

So, you want to have these changes into your local working directory.

You would need to pull updates from a remote repo to your local repository so you can get the updated code.

git pull origin {branch name}

Git fetch command – fetch all updates in remote tracking branch

Git fetch command pulls the updates from the remote repo and creates a new branch with reference to a remote branch.

git fetch origin {branch name}

If you are doing ‘git fetch’ first time for a branch then it will first create the reference of remote tracking branch. Let’s understand this by example. You are working on a feature and some other programmer also working on some related feature.

You just want to see what updates he made before merging in your local working directory.

To check, what updates made by others you can do’ git fetch’ origin branch_name.

git fetch origin branch_name

This will create a new branch origin/branch_name as a reference of remote branch.

Git branch – use git systematically by branches

If you are using the git, you must use branches.

What is a branch in git?

You can divide your project into multiple modules and a module can be divided into multiple features. Whenever you have to start coding a new feature or making some changes, you can create a branch to encapsulate the feature. Doesn’t matter if it is small or large.

Let’s say I am working on an e-commerce website and have to integrate the FedEx as a courier partner, so before starting code, I will create a branch named “fedex-courier”(You can name it as per your convenience, but the name should be good enough to tell what is this branch about)

Note: Before creating a new branch you should take care that what branch code it will copy. It’s a common practice to create the new branch from master branch.

Git branch – that will list your local branches

How can you see what branches your local repository has?

You can use git branch command, which will display all branches of local repository.

git branch

//It will list of all local branches
Note: Current working branch will be denoted with asterik.

If you want to see the branches of remote repository also then you will have to use git branch -a command.

git branch -a

Create a new branch

Find command below to create a new branch.

git checkout -b {new branch name}

You should check what is your current branch (use git branch—remember asterisk) before creating a new one. Because, the new branch will be copied from your current branch—You can see what important it is as I repeated it again.

Create and copy the remote branch

You might need to copy the remote branch into your local repo. There is 2 way for this:

  • Create a branch on local and then pull the remote branch
  • Directly checkout the remote branch, below is command to checkout remote branch
git checkout origin {branch name}

How can you switch from one branch to another

You would need to checkout the another branch where you want to switch. If you are working on a branch A and then you have to do some changes in branch B, use git checkout to switch on branch B.

git checkout {branch name}

Git delete local branch

You might need to create some temporary branches to just review some feature. And, once the purpose completed, you might need to remove this from git. Use git delete command to do this.

git branch -d {branch name}

Git delete a remote branch

git push origin –delete {branch name}

Git diff command – see difference between 2 branches

When you integrate some other branch changes into your then you would need to see that what difference that branch have.

git diff {branch1}, {branch2} 
//show the difference between 2 branches.

Git merge command – merge one branch to another

We discussed that a feature should have a branch but at the last point all branches must be merged into one single branch which will be uploaded on a production server.

git merge {source branch}
{destination branch}

Git automatically opens an editor with a default message, You can either write your own message for log or you can go with the default.

Conflict in merge

When you are merging two branches, it works well if updates in both branches are in different files. But, if both branches have updates in the same file then it shows the conflicts and these conflicts you would have to resolve manually.

How to resolve merge conflict

While you are merging two branches, git tells you if there are conflicts. After merge, run git status command, you will see a label “both modified” with the files which have conflicts.

When you open the file; you will find some additional tagging to identify the conflict.

<<<<<<< destination-branch-name 
...changes from the destination branch...
=======
...changes from the source branch...
>>>>>>> source-branch-name

OR it might be:

<<<<<<< HEAD 
...changes from the destination branch...
=======
...changes from the source branch...
>>>>>>> source-branch-name

Content after “<<<<<<<” are changes from destination branch and content after “=======” are your changes.

To resolve a conflict; you need to edit the file and then remove these markers and keep the necessary code and delete duplicate or unwanted lines.

Commit the changes when conflict are resolved.

Git checkout command – create or switch a branch

Git checkout command is used to:

  1. create a branch
  2. Switch to another branch
  3. Restore an old file from git
  4. Undo
    1. Undo an un-staged file
    2. Undo and staged file
    3. Undo the all staged file from a directory

Create a branch

 //create a new branch
git checkout -b {new branch name}
git
checkout -b {branch name} origin/{branch name} 
//It will create a new branch and then will clone remote branch to a new branch in local repository

Switch to another branch

//switch another branch
git checkout {branch name}

Restore an old file from git

You or someone else might messed up the code of a file and now you are looking to restore an older version of that file.

First, you have to find out in which commit id you would like to get the older version of this file.

You can find the commit hash id either from the UI of your central repository (GitHub, Bitbucket, gitLab) or can get commit hash id by running git log command.

Once you have commit hash id, run the command given below to restore the old version of a file.

git checkout {commit hash id} {filename}

Undo an unstaged file

You made some changes in a file and Some time after; you realized to undo the changes, if the files in not added in stage area then you can simply checkout that single file which will get you in original state.

git checkout --{filename}

Undo the staged file

Once the file is added into staged area, you need to do 2 steps to undo the changes:

  1. Move file back in to un-stagged state
  2. Use git checkout –filename to undo the unstage file

Move file back in to un-stagged state

Command:

git reset HEAD  {filename}

Undo the unstaged file

Git reset HEAD will change will reset the last commit into un-staged area and then you can use git checkout –filename to get original state of file.

Command:

git checkout --{filename}

Undo the all stagged files from a directory

We have looked how to undo a single file. Sometimes, you need to undo the changes of all files from a directory.

You will again have to do 2 steps for this:

Step#1 Unstaged the complete directory

git reset HEAD {directory name}

Step#2 Checkout the complete directory

git checkout – {directory name}

Git stash command – save staged or unstaged files in to a different tray

Sometimes, you are working on a feature and suddenly you have request to work on some old feature. You are not in state to commit the current code so what will you do?

Yes, you are thinking right. You would use git stash command to side line your current working files.

Command:

git stash

Let’s understand  it by  some examples:

Look the images below, I taken the screenshot fo git status before doing stash. It shows 3 files(1 in stage area[see green] and 2 in unstaged [red one]) in tracked area and 3 files in untracked area.

git status before stash

Now, lets do git stash. It will remove the all tracked file from here and will add in a hidden tray.

See image below, git status after stash.

git status after stash

You see, it removed all tracked files and now we only have untracked files.

Now, you can make changes in files, and then commit those.

Git stash pop command

Now to get back in the initial state use git stash pop command, it will get back your sidelined file again in your working state.

git stash pop

You can check these by running git status command. All 3 files which were in different tray came back to in working state.

Git stash clear command

If you do not want to get back the stashed files then you can clear the stash.

git stash clear

Git log command

You are using git to store the change history, and a easy navigate way to look into that history. Git log command is used to navigate through the commit history.

git log

Below is the list of options for git log command:

  1. Format the log output
    1. git log –oneline: Display a commit in one line
    2. git log –decorate – Decorating the logs
    3. git log –graph
    4. git shortlog
  2. Filter from commit history
    1. Find by date
    2. Find by author
    3. By number of last commits
    4. Find by message
    5. Find by file
    6. Find by content: -S”<string>”
    7. Find by commit hash range
    8. Filter merge commits

# git log –oneline

When you run the git log command, it shows commit id, author, date, commit message for every commit. That makes it hard to read out all commits.

Git log –online list a commit in only one line.

#git log –decorate

By default, git log do not show the branch name with commit so it become hard to identify that what branch the commit is related to.

Git log decorate show the branch name with the commit. Best to use it with –oneline option.

git log –oneline –decorate

# git log –graph

Graph option draw the ASCII graphs which represent the branch structure of the commit history.

Its recommended to use it with oneline and decorate options for better representation.

git log –graph –oneline –decorate

# git shortlog

If you want to check who is working on what, then this command will help you. It group each commit by author and display the first line of commit message.

Filter commits by date, author, file name, message etc.

# Find commit from log by date

Find commits after a date:

git log --after={date}
git log –after=”2018-07-25”

You can also use relative reference like yesterday, “1 month ago” etc.

Find commits before a date

git log --before={date}
git log –before="2018-07-25"

Find Commits between a date range

git log --after="2018-07-15" --before="2018-07-25"

# Find commits by author

This command is useful when you are looking for commits created by a person.

git log –author="Mohan"
//search for multiple users
git log –author="Mohan \|Shyam"

# By number of last commits

Some time you want to see only last n(any number) commits.

git log -5
//will return last 5 commits

# Find by commit message

git log –grep command can be used to find the commit by commit messages.

Example: I usually write first the storyline of the article and then commit it so for each article I have commited message with text “storyline”. Now, I want to see all storylines versions.

git log –grep="storyline"

# Find commit by file

Sometimes, you need to see all the commits related to a file.

git log – file1.php

Git blame or git annotate command – find who write what line

Git blame command shows who was the last person to change each line of a file. That take file name in the comamnd and then show each line along with author name.

It happened many time when I think who written this code. After using the git blame I knew that it was me 🙂

You can find this option in GitHub, GItLab and BitBucket.

Git show command

This command helps you to get the previous commit from history.

  • git show {few character of commit hash} – Find a specific commit by providing the few character of commit hash
  • Git show HEAD – It will display most recent commit
  • Git show HEAD~1 – It will display one commit before the most recent comment
  • Git show HEAD~2 – It will display 2 commit before the most recent comment

Git rebase command

So far, you might be using merge to integrate changes from one branch to another. Merge worked only one way, it integrates the changes which are new for a branch, If you have to rewrite the commit from the history then REBASE would be used.

Let’s understand git rebase by example.

Assume, you are working on a branch feature1. Some other developer integrated some commits into the master branch. And, these changes are related to your task so you would like to have it in your branch feature1.

To integrate this newly pushed commit into your branch, you would use git merge.

git checkout {your branch}
git checkout master

Git merge will integrate the new commits into your branch and will create a new merge commit.

Git rebase also integrate the other branch commits to your working branch.

So what is the issue in git merge? Why should I use git merge?

Hang on!

You see, git merge creates a commit for merge, so if you frequently get updates in the master branch from others which are useful for you, so doing frequently merge can pollute your branch and it can become hard to understand the history of the project.

Git rebase is another option to integrate the changes into your branch. It rewrites the history by creating new commits for every commits you to receive from master branch. So you would have a cleaner history with rebase.

Will update it later for further reading….drop me an email on rakesh.shekhawat@gmail.com if you want to have early access of google doc where I have rough storyline for further part.