Command Line Git: Init and Add Remote Repo

I may be a bit of a noob, but I’ve generally just always used some sort of GUI program when it came to git. It always did what I needed, so I never really put much thought into using the command line.

Well, at work I had to set up new git repos for a bunch of projects. Using a program was just too annoying. All I needed to do was initialize my local git repository, add the remote, and then push my initial commit.

Well, turns out you can do that in six nifty command lines.

It’s worth noting that I’m on Windows

Init Git

This is one of the most simplest things to do, it’s literally two words.

git init

Navigate to your directory using the cd command. Once you are there, you just need to type in git init and you are rolling.

Add Remote Repository

git remote add origin <url>

Next we need to tell git where our remote repository is going to be. Gitlab makes it super simple to find and copy the url. Once we use the command, replacing <url> with the url of the remote repository, we are golden.

Play Fetch

git fetch --all --prune

Once we have our remote repository set up, we do a fetch just to pull down all of the commits to the local repo. I get super confused between fetch and pull, but from my sifting around, a pull is a fetch and merge, whereas a fetch just pulls the commits without merging.

Commit and Push

git add -A
git commit -m "Initial Commit Message"
git push origin master

The last thing we have to do is pretty standard with git. It’s the same commands you’ll do whenever you make a change to your local copy. First, we add all of the files with add -A, then commit them with a message. Finally, we do a push. If your remote repository is empty, this will automatically create a master branch.

If you want to see it all together:

git init
git remote add origin <url>
git fetch --all --prune
git add -A
git commit -m "Initial Commit Message"
git push origin master

That’s It

As I said before, git is certainly not my strong suit. In fact, I had to look up these commands just to make sure I’m doing them right. I also have them all on a sticky note attached to my monitor so I don’t forget.

Practice makes perfect, right?

Now if only I could have a branch merge work without breaking something…that would be great.

Thanks for reading.

You Might Also Like...

Coding

Coding

Coding