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.
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
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
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.