Introduction to git for Beginners (Tutorial)

Git is one of the best Version Control Systems 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
      Screenshot of Visual Studio Team Explorer showing the Branches pane. The context menu for main branch is open, highlighting the option "New Local Branch From..." while utilizing Git slash commands.
  3. Name the new branch as ‘feature/#name_of_the_story_or_bug
    Screenshot of the Team Explorer window in Visual Studio showing the creation of a new git branch named "feature/#2564_Create_new_user" from the "main" branch with the "Checkout branch" option selected.

Commit changes to your new feature branch:

  1. To see your changes go to the Team Explorer tab and select Changes.
    Screenshot of Team Explorer in a development environment, showing options like Changes, Branches, Pull Requests, Sync, Pulse, Graphs, Issues, Fork and Settings. The Changes option is highlighted with the familiar git slash icon nearby.
  2. Add meaningful comments to your Commit and also associate a work item with the commit:
    • Add comment
    • Stag changed files
    • Click Commit Stagged
      Screenshot of Team Explorer in Visual Studio showing staged commits for the branch "feature/#2564_Create_new_user" with the message "Added all the features to create user," demonstrating a successful git workflow.

Merge the latest main branch to your feature branch:

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

  1. Make sure you commit every change to your feature branch.
  2. Switch over to ‘main’ and pull the latest version.
  3. Switch back to your feature branch and right click ‘Merge From …’
    Screenshot of the Team Explorer - Branches window in a Git repository. The context menu is open with "Merge From..." highlighted under a feature branch named #2564_Create_new_user, showcasing how to navigate git slash operations efficiently.
  4. Select main in ‘Merge from branch’ and select ‘Commit changes after merging’
    Screenshot of Visual Studio Team Explorer showing a git merge operation from the main branch to the feature/#2564_Create_new_user branch, with "Commit changes after merging" option checked and the "Merge" button selected.

Save changes (git stash):

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

  • git stash   // save pending changes
  • git stash list   // list saved changes
  • git stash apply   // apply the 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:

saved-changes

To apply the first saved change:

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

Also Read: PIVOT and UNPIVOT Operator in SQL Server – C# Example Code

Visual Studio 2015 problems:

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 the 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 the merge issue.

Sometimes you cannot merge the 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

Useful links:

  • VSTS Git Tutorial(https://docs.microsoft.com/en-us/vsts/repos/git/gitworkflow)
  • Git command line(https://docs.microsoft.com/en-gb/vsts/repos/git/mapping-my-tfvc-actions-to-git?)
  • Pull requests(https://docs.microsoft.com/en-us/vsts/repos/git/pullrequest)
  • Git command line reference (git-scm)(https://git-scm.com/)
Article by

Jignasha Rathod

Jignasha Rathod is a Technical Analyst with over a decade of experience in the IT industry. She excels in .NET, CI/CD, GitHub, Azure. and has a proven track record in project management, leadership, API integrations, and Azure AI and ML.net . Jignasha is focused on performance enhancement and possesses deep domain expertise in open source CMS ( umbraco, orchard cms ) accounting, CRM, ERP (SAP, NetSuite, Business Central) and e-commerce. Her extensive experience spans across both B2B and B2C e-commerce platforms, and she is leveraging AI and ML technologies to drive innovation and efficiency in client projects.