Introduction to git for Beginners (Tutorial)

By: Jignasha
Updated: December 18th, 2019 | Technology: Asp.Net
Introduction-to-Git

Git is one of the best Version Control System which is used in software development.

Advantages Of Git:

  • Faster
  • Only one directory per repository
  • Can work with branches, developers can work on multiple tasks with branches if needed
  • Versions can be managed within a single directory
  • Allows to create a branch, commit code locally
  • Can copy changes from one branch to another branch easily
  • Can use commands to work with git

Get the latest on ‘main’:

  1. switch to ‘main’ branch by double-clicking the branch
  2. click Pull to get the latest version

Create a new feature branch:

  1. Right-click ‘main’ and select ‘New Local Branch From…’
  2. Naming convention for feature/bugfix branches: ‘feature/#TaskId_title_of_work_item’ or ‘bugfix/#BugNo_ID_title_of_bug’
    • Do not use spaces or special characters in the feature name
    • The ‘feature/’ and ‘bugfix/’ prefix is lowercase
  3. Name the new branch as ‘feature/#name_of_the_story_or_bug

Commit changes to your new feature branch:

  1. To see your changes go to Team Explorer tab and select Changes.
  2. Add meaningful comments to your Commit and also associate a work item with the commit:
    • Add comment
    • Stag changed files
    • Click Commit Stagged

Merge latest main branch to your feature branch:

When branch is ready, first merge latest ‘main’ to your feature branch.

  1. Make sure you committed every change to your feature branch.
  2. Switch over to ‘main’ and pull latest version.
  3. Switch back to your feature branch and right click ‘Merge From …’
  4. Select main in ‘Merge from branch’ and select ‘Commit changes after merging’

Sometimes there are pending changes in your work space you don’t want to commit but want to save. You can shelve changes with git stash. To save pending changes:

  • git stash   // save pending changes
  • git stash list   // list saved changes
  • git stash apply   // apply latest saved change
  • git stash drop   // delete latest saved change
  • git stash clear   // delete all saved changes
  • git stash pop   // apply the stash and immediately drop

In this example I have two saved changes:

To apply the first saved change:

git stash apply: git stash apply stash@{0}

Resolve error message about uncommitted changes

Sometimes you cannot switch between branches, or cannot merge / pull as VS2015 shows an error about uncommitted changes – however when you look at “Changes” there is nothing pending.

This is a VS2015 issue and to get around it, do the following:

  1. Open a Git bash command line in your git / ODS eCommerce folder
  2. Run command: git status
  3. Confirm that there are no pending changes
  4. Go back to VS2015 and try to do your merge / pull / etc action again – it should be fine now

Resolve merge issue.

Sometimes you cannot merge latest main into your feature branch even after you did a “git status”.

In this case you can fall back to your git command line and try merging there:

  1. Switch to the target branch you want to merge to
  2. Open Git bash command line in your git / ODS eCommerce folder
  3. Run “git status” and confirm there are no pending changes
  4. Run “git merge main
  5. Go back to VS2015, allow reload all changed files and go to “Changes
  6. You might have conflicts to resolve
  7. If there are no conflicts then Commit your changes

Refresh local feature branches when they deleted on VSTS.

At the end of a pull request feature and bugfix branches going to be deleted by default.

VS2015 does not automatically exclude deleted remote branches until you run this git command.

cmd:

  • git remote prune origin –dry-run
  • git fetch –prune
  • -OR-
  • git remote prune origin