Let’s Git Started . . .

Suhas-Gavad-photo

Suhas Gavad

08 Dec, 2017

What is Git and why we are using it?

Git is a Version control system, specifically a version control system for tracking changes in computer files (mainly in project directory). It is designed to manage source code mainly, which enables team members to work on the same code at the same time without overwriting each other’s changes as well as to keep track of individual changes as a separate version.

To start with Git need to make project directory as Git repository usually we called it as ‘Local Repository’. To do this we can use ‘git init’ command.

Cmd: git init

This command is used to create the new git repository, simply it initiates new repository where we can store the project files and track them easily.

Git setup command 

We need to do some basic ‘set-up’ for git like every tool/software needs to do while installation. This is the first tasks while setting up your Git environment. It allows you to instruct Git like any other software that how you want it to function for you. After initializing Git in the directory you should link your username and email address to your Git profile. You can use the following command to do same.

 git config --global user.name "Suhas Gavad" 
 git config --global user.email "suhas.gavad@tudip.nl"

There are the level of configuration we can do, one is with –global option which basically works as the default configuration for all git repository setup on your system. Another one is with –local option which only applies to the repository where you are running this command.

Git adds this information to each of your change and operations in git and let others people(who are working on same repo/code/project) identify which changes you made. It allows you to direct Git that how you want it to function for you.

Read More …

How does Git work?

Basic Git commands that usually needs to perform Git operations:

Create Commands

  • Clone an existing project/repository.
  • Cmd: git clone <remote url>
  • Create a new local repository.
  • Cmd: git init

 

Local Changes Commands

  • Check the changed files in your working directory
  • This command is used to check the status of project files whether they modified or not. If files were modified then it will shows the list of files/directories which gets modified.

    Cmd: git status
  • Check the changes to tracked files
  • This command is used to check the difference bet the last version of files and the current version of modified files.  Simply shows the newly added/removed/modified contents in repository/project/files.

    Cmd: git diff
  • Add current changes to the next commit
  • This command is used to add the newly added or modified files to current branch.

    Cmd: git add <filepath>
  • Commit staged local changes in tracked files
  • This command is used to commit the recently added files to the branch and save changes to project/repository. Options we can use with : ‘-m’ to specify commit message.

    Cmd: git commit

 

Branches and Tags Commands

  • Check current branch.
  • This command is used to check the current branch and to display the git branch names list available.

    Cmd: git branch
  • To list all existing branches.
  • Cmd: git branch -av
  • To switch between brach.
    ‘git checkout’ command usually performs operations related to git branch/files. Variations that we generally used with this are:-

    1. To switch from one to another branch.
      Cmd: git checkout <branch name>
    2. To create and switch into new branch.
    3. Cmd: git checkout -b <new-branch name>
    4. To remove all the modified changes from the specified file.
    5. Cmd: git checkout <fileName/path>
  • Delete a local branch 
  • This command is used to delete the branch from local repository.

     Cmd: git branch -d <branch>
  • Delete a branch on the remote
  • Cmd: git branch -dr <remote/branch>

 

UPDATE & PUBLISH Commands

  • Fetch the changes and directly merge/integrate into HEAD.
  • This command is used to pull the project/files/code from the remote location.

    Cmd: git pull <remote> <branch>
  • Publish your local changes on a remote repository
  • This command is used to publish the committed changes on remote location. Options we can use with : ‘-f’ to forcefully publish changes.

    Cmd: git push <remote> <branch>

 

MERGE & REBASE Commands

  • Merge <branch> into your current HEAD/branch.
  • It simply merges one branch into parent or master branch.

    Cmd: git merge <branch>
  • Rebase your current HEAD onto <branch>
  • Cmd: git rebase <branch>

    git rebase master

    git pull --rebase

  • Abort a rebase
  • Used to abort the rebasing operation.

    Cmd: git rebase --abort
  • Continue a rebase after resolving conflicts
  • Continues the rebasing process only when all the conflicts get resolved.

    Cmd: git rebase --continue

Read More …

UNDO Git Operations Commands

  • Discard all local changes in your working directory
  • It removes all the files changes.

    Cmd: git reset --hard HEAD
  • Discard local changes in a specific file
  • Restores all the changes to file up to the last commit.

    Cmd: git checkout HEAD <file>
  • Reset your HEAD pointer to a specified commit and discard all changes since then
  • Removes all the changes done after the commit ID specified in the command.

    Cmd: git reset --hard <commit>
  • Keeps all changes as unstaged changes
  • This will remove the commit but keep the committed changes to the staged area on the local repository.

    Cmd: git reset <commit>

 

COMMIT HISTORY Commands

  • Show all commits, starting with newest commit information
  • Shows sequence of commits and respective commit information.
    Example. Email and commit reference ID.

    Cmd: git log
  • Show changes over time for a specific file.
  • Cmd: git log -p <file>
  • Who changed what and when in <file>
  • Cmd: git blame <file>

Advanced Git operations

Rebasing – Git provides two ways to integrate/migrate changes from one branch to another branch, i.e. ‘merge’ and the ‘rebase’ operations. you can take all the changes that were committed on one branch and can merge/integrate them on another one.

What-Is-Rebase

This process automatically rebases the current branch onto <base>, which can be any kind of commit reference (for example an ID, a branch name, a tag, or a relative reference to HEAD). This allows you to clean the git-commit history by removing/splitting/altering an existing series of commits.

Step 1)

Cmd: git rebase -i <last commit number from which you want to rebase>

Step 2)

Cmd: git pull --rebase origin <master branch name>

Step 3)

Cmd: git push origin <current branchname>

After hitting step 1 command it will open the squash terminal/window in which we can modify commit info. It will display the commits did/made after the last commit number you entered. And also specify few operations to perform exactly below the list of commits.

At this step after squashing, if any conflict occurs then the rebasing process will get pause until unless we resolve the conflicts. After resolving the conflicts we need to do ‘git rebase –continue’ to continue the rebasing process.

What-Is-Squash

Note: Do not squash commits that you’ve already published with others. You’re changing history which will cause trouble for others. You can abort the rebasing process using ‘git rebase –abort’ command.

Read more …

Example. Explaining with simple git project operations.

You can sequentially follow below steps to perform git operations on any project.

  • Clone repository. Cmd: git clone <remote url>
  • Pull down the repository. Cmd: git pull
  • Check first for the current git branch. Cmd: git branch
  • Starting with new branch. Cmd: git checkout -b newBranchName
  • Adding the modified files to the current branch. You can check the modified files list using `git status`. Cmd: git add <modified file path>
  • Committing the added files to current branch. Cmd: git commit -m or git commit
  • Publishing your changes into your repository. Cmd: git push

Tips that might helpful

  • Find branch having commit “sdf54sd5f4s6d54f65sd54f65sdf46d54f4”
  • Cmd: git branch --contains sdf54sd5f4s6d54f65sd54f65sdf46d54f4
  • Edit git config.
  • Cmd: sudo nano .git/config
  • Remove added file but not committed.
  • Cmd: git reset HEAD
  • Undo last commit (only on local).
  • Cmd: git reset HEAD~1
  • To determine who made changes to a file.
  • Cmd: git blame <file path>
  • To check the difference in file or modified code in file.
  • Cmd: git diff
  • To Delete a local branch.
  • Cmd: git branch -d <branch name>

You might require changing the commit's author name/commit_comments, so you can follow these steps.

For example, if your commit history is 1-2-3-4-5-6 with 6 as HEAD, and you want to change the author of 3 and 4, then you would...

  1. Specify git rebase -i 2 (note: if it's 1 you need to edit, use git rebase -i --root)
  2. Change the option for both 3 and 4 to edit. i.e. at the start of specific commit line write 'e'.
  3. Once the rebasing started, it would first pause at 3 to make specified changes.
  4. Then you would do git commit --amend --author="Author Name <email@address.com>" to modify the commit info.
  5. Then git rebase --continue. It would pause again at 4
  6. Then you would git commit --amend --author="Author Name <email@address.com>" again
  7. git rebase --continue
  8. After completing the rebasing it would get changes to newly modified commit info.

There are many interfaces available for Git like GitHub, GitLab, Savannah, BitBucket, and SourceForge etc.

GUI Tools available to simplify Git operations:

  1. GitKraken (Windows, Mac, Linux)
  2. GitHub Desktop (Windows, Mac)
  3. SmartGit (Windows, Mac, Linux)
  4. Tower (Windows, Mac)
  5. git-cola (Windows, Mac, Linux)
  6. CodeReview (Windows, Mac, Linux)

Refferance Links:

http://rogerdudler.github.io/git-guide/
https://opensource.com/resources/what-is-git
https://confluence.atlassian.com/bitbucketserver/basic-git-commands-776639767.html
https://www.git-tower.com/blog/posts/git-cheat-sheet
http://rogerdudler.github.io/git-guide/