Yesterday the program worked. Today you changed three things, it no longer compiles, and you can’t remember which three. Without history, all you can do is undo by hand and hope.
Git solves that. It keeps snapshots of your project over time, and you can go back to any of them. In this chapter you will install it, save your first project, work on a separate branch and push it to GitHub. Everything can be done from the terminal and from VS Code, so I show both ways side by side.
Git and GitHub are not the same thing
Git is a program that runs on your computer and tracks the history of your files. GitHub is a website where you can keep an online copy of that history, share it and collaborate with other people. You can use Git without GitHub. GitHub without Git makes no sense.
Three words you will hear all the time:
Repository: your project folder plus its whole history.
Commit: a snapshot of the project at one moment, with a message that explains what changed.
Branch: a parallel line of work, so you can try something without touching what already works.
Install Git and check that it works
Open the VS Code terminal (Terminal > New Terminal) and type:
git --version
If it prints a version number, you have it. If it says the command does not exist, install it from git-scm.com, close VS Code and open it again. The VS Code documentation says it uses that Git installation for everything it does in source control.
Before your first commit, tell Git who you are. This name and email are written into every commit you make:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
--global means it applies to all your projects. You do it once per computer.
Create a project and its repository
We will use the corner store from the earlier chapters. Create the project and go into the folder:
dotnet new console -n CornerStore
cd CornerStore
Open the folder in VS Code (File > Open Folder). Now turn the folder into a repository:
git init -b main
git init creates a hidden .git folder where Git keeps the history. The -b main option names the initial branch main. According to the git init documentation, without that option the name comes from the init.defaultBranch setting, and if that is not set, the current default is master.
In VS Code: open the Source Control view with Ctrl+Shift+G (Ctrl on Mac too) and click the Initialize Repository button. If you don’t see it, open the Command Palette and run Git: Initialize Repository. This path uses the default branch name from your configuration. If you got master and want main, run git branch -M main in the terminal, which renames the current branch.
Ignore what does not get saved
When you build, .NET generates the bin/ and obj/ folders with files that are recreated on their own. They don’t belong in the history. You tell Git with a .gitignore file, and .NET can create it for you:
dotnet new gitignore
According to the .NET CLI documentation, that template (gitignore) creates a .gitignore file. Open it and look for bin/ and obj/: they are already there, along with many other typical .NET rules. Do this before the first commit. If you already saved bin/ and obj/, Git keeps tracking them even if you add the rules later.
Check the status, stage and save
git status
You will see something like this (trimmed):
On branch main
No commits yet
Untracked files:
.gitignore
CornerStore.csproj
Program.cs
Untracked means Git sees the files but is not following them yet. Saving a change takes two steps. First you stage what goes into the next snapshot, then you commit:
git add .
git commit -m "First commit: base project"
git add . stages everything in the folder except what .gitignore excludes. Run git status after git add and before the commit: you should see the files in green and no bin/ or obj/ folder. If you prefer to control it file by file, type the names: git add Program.cs.
The -m message is one short sentence saying what changed. “Changes” helps nobody, including you a month from now.
In VS Code: the Source Control view lists the files under Changes. Hover over a file and click the + icon (Stage Changes); the file moves down to Staged Changes. Type the message in the text box at the top and click Commit. It is the same add plus commit, with buttons.
To see the history, back in the terminal:
git log --oneline
7b2f0a3 First commit: base project
That mix of letters and numbers is the commit identifier. Yours will be different.
Work on a branch
Say you want to add a discount to the program and you are not sure it will work out. Instead of risking main, you create a branch:
git switch -c discounts
According to the git switch documentation, -c creates the new branch and switches to it in one step. Edit Program.cs, run git add Program.cs and commit with git commit -m "Add quantity discount". That commit lives only in discounts. To go back to the main branch, use git switch main, and you will see your file as it was before the change.
In VS Code: in the Command Palette run Git: Create Branch… and type the name. The status bar at the bottom left shows the current branch; click it to open a list for switching branches. There is also a Git: Checkout to command.
Bring the work together with merge
When the discount works, you bring it into main. You stand on the branch that receives the changes and tell Git which one to integrate:
git switch main
git merge discounts
Because main did not change while you worked, Git just moves main forward to your latest commit (a fast-forward). If both branches changed the same lines, Git can’t know which version you want and flags a conflict. VS Code’s Source Control view shows it and lets you resolve it in the editor; for now, just remember that it exists and that it is not your mistake.
In VS Code: switch to main and run Git: Merge… from the Command Palette. Pick discounts.
Push the project to GitHub
So far everything lives on your computer. If the disk dies, the history goes with it. GitHub gives you a copy somewhere else.
- On github.com create a new repository (New repository). Name it
corner-store. Leave it empty: no README and no.gitignore, because you already have yours. - Copy the URL GitHub shows you and connect it to your local repository.
originis the conventional name for the main remote:
git remote add origin https://github.com/YOUR-USERNAME/corner-store.git
git push -u origin main
git remote add registers the address. git push sends your commits, and -u remembers that local main tracks main on origin, so next time git push is enough. On the first push, VS Code asks you to sign in to GitHub; you don’t need an extension to authenticate Git operations.
In VS Code: run Publish to GitHub from the Command Palette. According to the documentation, this step is optional and requires a GitHub account. It creates the remote repository and pushes for you.
Never push secrets
Once something is in a commit, it stays in the history even if you delete it in the next one. If the repository is public, anyone can read it. Passwords, API tokens, database connection strings and .env files do not get committed. Put them in .gitignore before you create them, and if you ever push one by accident, rotate it at the source service: deleting the commit is not enough, because someone may already have copied it.
Exercise: the full cycle
With the CornerStore project you already have:
- On the
mainbranch, confirm withgit statusthat nothing is pending. - Create a branch called
greeting. - In
Program.cs, add a line that writes a welcome message withConsole.WriteLine. - Stage the change, commit it with a clear message and look at
git log --oneline. - Go back to
mainand mergegreeting. - Push it to GitHub and check that the commit shows up on the site.
Suggested solution
git status
git switch -c greeting
# edit Program.cs and save the file
git add Program.cs
git commit -m "Add welcome message"
git log --oneline
git switch main
git merge greeting
git push
The last git push works with no extra arguments because you used -u earlier. If you get a command wrong, nothing is lost: git status always tells you where you are and what comes next.
Recap
- Git keeps the history on your computer; GitHub keeps an online copy.
git configwith your name and email is done once;git init -b maincreates the repository.dotnet new gitignorekeepsbin/andobj/out of the history.- The daily cycle is
status,add,commit -m, andlog --onelineto review. git switch -ccreates a branch, andgit mergeintegrates it from the receiving branch.git remote add originplusgit push -u origin mainpush the project; VS Code offers Publish to GitHub.- Secrets never go in a commit.
In the next chapter we cover something you will use every day without anyone teaching it: how to read the official documentation so you can find the answer yourself.
Stuck on a step?
DM me on Instagram or ask in the community on Discord. Tell us which command you got stuck on and what message you saw.
