Skip to content
2

Git: Essential Commands for Visual Studio Developers

Unfortunately, I see a lot of devs who would like to learn git, but get stuck trying to get to Git Level 1.

This post is the list of the essential commands that I believe you know to get started.

 

Getting Help

The git help is really good once you have an understanding of what’s going on.

git help                 : return list of all the commands
git help <command name>  : to return the options for a specific command
git help add             : to display help specific for the add command

The Basics

Visual Studio  does a great job of the basics. All of the commands in this section are handled very nicely in the Visual Studio interface. But….. it will go wrong, and when it does you are going to need to go to the command line. I’d strongly suggest that you spend at least a week or two doing the basics on the command line, as it will help you with your understanding of git, so that when things to get more complicated, you can deal with them.

Create a new repository

git init

Add a file to the repository

Once you add a file to the folder, you need to tell git that you want git to track it.

git add <pathspec>          : Adds all the files in the specified path
git add *.cs                : To add all c# files

Tip: to add all files in the working tree that have been added or changed, but are not excluded by the .gitignore file.

git add .

Note: <pathspec> is used to indicate a file path. Wildcards can be used. e.g. .cs and also leading directory names. e.g. content/.jpg.

Status, Commit, Push, Pull

Just the basics will get you a long way here. Check the references below for great articles explaining in more details.

git status

git commit -m "Merged suo conflict from staging"   : Commit to the local repository 

git pull                       : Pull the pending commits from the server 
git pull origin staging        : Same as the above, but specifies the server and branch to pull from

git push                       : Push the current repository
git push origin staging        : Some devs like to specify the target repository as well. Here it is 'staging' on 'origin'

Merge from staging to master

First you must be on the branch that you want to merge to git checkout master :The checkout command select the branch to work on Merge the staging branch into the master branch

git merge staging           :Specify the branch to pull the changes from

Commands You Wish Visual Studio Had

Discard your local changes. (Equivalent of Visual Studio, Source Control, Undo – but this one will always work*)

Are you in Visual Studio, trying to undo all your changes, but getting an error. This is the answer.

This command will

  • point the current branch back to point HEAD (your last commit).

  • update the files in the staging to match those from HEAD.

git reset --hard HEAD

 

Remove files from your repository (so that they aren’t tracked), but leave them in the working directory.

There are files in your solution that you don’t want git to track.

To stop git from tracking these files you should

– tell git not to track them in the future by adding an exclusion in your .gitignore file TO

You dont’ want to include your user settings files in your repository because it is individual to each user.

If someone has committed it to your repository, you want to remove it from the repository so that it is no longer tracked by git, but still leave the files in the working directory.

git rm *.suo --cache

The same applies to your Azure publish profiles. Remove the azure publish setting files from the index, but leave them in the working folder

 git rm *.pubxml --cache

Check out my post on updating your .gitignore file

More to come…

Keep checking back as I’ll add more as they come up and I think they are helpful.

 

Great References

Learn Git in 15 Minutes

Try Git from CodeSchool

Github for Beginners: Don’t get scared get started.

Git Reference

Everyday Git with 20 commands or so

 

Advertisements
2 Comments Post a comment
  1. May 2 2016

    I want to change my background colors back to windows normal color and I don’t know how. I can’t find the place that I did it.

    Reply

Trackbacks & Pingbacks

  1. Git for Visual Studio Developers–Get your .gitignore right and save yourself needless merges | adamstephensen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments