Using git as a Source Code Managment (SCM) tool is a great way to manage source code. While creating a example console application to demonstrate how to use
System.CommandLine I documented most of what I was doing. I had originally included a lot of detail in that documentation.
After completing the documentation I decided that while it might be useful to me or someone else in the future, having all of that information in a single post was just too much. I decided to break up the content into smaller more managable parts. This is one of those parts.
- Initialize source repository
- Add a .gitignore and README
- Add initial template files
- Create branch and add remote
Initialize source repository
Now is a good time to initialize your source repository. I am going to use a
git repository to manage the source. If you are using a different source code management (SCM) tool to manage your source then be sure to use the respective commands for you tool. The steps are basically the same for all SCM tool.
Ensure that your are in the root directory for your new project and initialize the repository.
$ cd ~/SimpleCmdLine $ git init Initialized empty Git repository in /Users/iesoftwaredeveloper/repos/SimpleCmdLine/.git/ $
Add a .gitignore and README
To help ensure that we avoid committing files that we don't usually want in our git repos we should create a
.gitignore file. Fortunately,
dotnet can help us create a simple
.gitignore that will accommodate some common files when developing using Visual Studio. If you are using Visual Studio Code the same
.gitignore will also apply. If you are using a different Integrated Development Environment (IDE) or using a simple text editor then you will still benefit from creating the
.gitignore using this method. Many of the items are created as part of the
To create a dotnet focused
.gitignore using the
dotnet new gitignore command. While we are here I will create a basic README.md as well.
Note: The readme template is not installed by default. If you do not have it installed then dotnet will search NuGet for a matching template. I installed the template from author Rodolpho Alves named "Readme (Markdown)". You can install it using the command
dotnet new -i ARDC.Commons.Templates::0.2.1This is entirely optional and you can certainly just manually create your own README.md
$ dotnet new gitignore The template "dotnet gitignore file" was created successfully. $ dotnet new readme The template "Readme (Markdown)" was created successfully. $ git add .gitignore $ git add README.md $
Add initial template files
Now that we have initialized the git repo and added a .gitignore, go ahead and add the intial files created by template and commit them. Since there are only two of files I will add them individually. Once the files are added we can commit the changes to the repo.
$ git add src/Program.cs $ git add src/SimpleCmdLine.csproj $ git status On branch main No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: .gitignore new file: README.md new file: src/Program.cs new file: src/SimpleCmdLine.csproj Untracked files: (use "git add <file>..." to include in what will be committed) $ git commit -m "Initial commit"
You could add all of the files in the directory using
git add .or
git add srcto add the files in the src directory.
Create branch and add remote
Now that we have made our first commit we can push our local repository to a remote. I want to make sure that the current branch which is the default is named
main to match the default branch name I use on my remote git repository. In this case it is, but if your git instance still uses a default branch by some other name this will ensure it is named
Once we have (re)named the local branch we can add the remote and push it to the remote repository.
Be sure to use your username for user and whatever you named your remote repository for repo
# Rename current branch to main (forcefully) git branch -M main # Set the new remote git remote add origin firstname.lastname@example.org:user/repo.git # Verify the new remote git remote -v > origin email@example.com:user/repo.git (fetch) > origin firstname.lastname@example.org:user/repo.git (push)
Now that we have made our intial commit and set the remote we can push the changes to the remote.
$ git push -u origin main Enumerating objects: 7, done. Counting objects: 100% (7/7), done. Delta compression using up to 16 threads Compressing objects: 100% (7/7), done. Writing objects: 100% (7/7), 3.97 KiB | 3.97 MiB/s, done. Total 7 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:iesoftwaredeveloper/SimpleCmdLine.git * [new branch] main -> main Branch 'main' set up to track remote branch 'main' from 'origin'. $
You now have a local repository and can push it to a remote repository.