Check in New Code to Github with 7 Git Commands

Master is no more! GitHub has done away with the term “master” because of of its racist association in American history. This post is not  about what I think about it, but rather the way it changes I push my source code to GitHub.  My post is for new repo only! This is an updated version of my old post from 3 years ago, where I talk about 6 git commands for first time code push.

My situation is a little simpler. I use GitHub just for my personal work. I don’t collaborate with anyone. I don’t use branches. I work on the master directly. I use Windows. If this is your situation as well, then this post might help you. Again, if you haven’t set up your environment, do the following:

  1. Install Git SCM on your computer to enable you to use git on the command line.
  2. Register at Github
  3. Create a new repository. The URL of your repo is what you will use step #4 below. The URL of your repo is that URL you see when you click the green button that says Code on the upper right corner of your repo page in GitHub.
URL of your Repo

The 7 Commands

Let’s get started! Open a Command Prompt or Powershell. Go to you local directory where your source code is and execute these commands in this order:

  1. git init
  2. git add -A
  3. git commit -m “your message”
  4. git remote add origin https://github.com/username/reponame.git
  5. git pull –rebase
  6. git branch -m master main
  7. git push -f origin main

Brief Explanation

git init

Initializes and prepares your directory for git. Suffice it to say that you need to open a command prompt or Powershell » CD to your source code directory » type git init and Enter. You can execute git status at any point in time to see where you’re at in the process.

git add -A

Prepares all changes on staging. The -A means all changes are staged including edits, additions, and deletes. I do this because I don’t have to think!

git commit -m “your message”

Commits all the changes. After commit, you are ready to push to Github.

git remote add origin https://github.com/username/repo-name.git

Associates your directory with a remote git server and repo. You can execute git remote -v to verify that your directory has been associated with your remote repo.

git pull –rebase

Note: “–rebase” is two dashes before “rebase”. This step is somewhat controversial. I only use this because I am pushing directly to a master, so I need to pull from the remote first before pushing. In fact, if I don’t use this, it won’t let me push! According to Atlassian, it’s like saying, “I want to put my changes on top of what everybody else has done.” So, this is perfectly fine for my situation.

git branch -m master main

Move the master branch to main. This step is what’s different from how I used to do things!

git push -f origin main

Because I did a pull –rebase, I need to force or use the -f directive. So, yeah, instead of “master”, use “main”!

Bonus

If you just want to commit changes, you just pretty much do the following:

  • git add -A
  • git commit -m “your message”
  • git push origin main

Remember there are variations of these commands. You can even do git commit -am "your message" to skip staging process, for example. But my goal in writing this tutorial is to make the process a little simpler to understand especially for beginners. Hope this helps!

Alejandrio Vasay
Alejandrio Vasay

Welcome to Coder Schmoder! I'm a .NET developer with a 15+ years of web and software development experience. I created this blog to impart my knowledge of programming to those who are interested in learning are just beginning in their programming journey.

I live in DFW, Texas and currently working as a .NET /Web Developer. I earned my Master of Computer Science degree from Texas A&M University, College Station, Texas. I hope, someday, to make enough money to travel to my birth country, the Philippines, and teach software development to people who don't have the means to learn it.

Articles: 21

Leave a Reply

Your email address will not be published. Required fields are marked *