How to Create a GIT repo from CLI - jsanderson104/GIT-CheatSheet GitHub Wiki
First we need to make a directory on our local system.
mkdir newrepo && cd newrepo
Initialize the directory for GIT, which creates the .git directory
git init
Make a README.md file to start tracking and to have at least one file that could be changed to the "staging" area. This means, we need to have something to track. There's nothing to track in an empty directory.
echo "HELLO WORLD!!" >> README.md
Now that we have something to track.. but just because we created a file in the "newrepo" directory doesn't mean it automatically started tracking your changes to THAT file. Let's run this command to see what "state" we're in as far as what GIT knows.
git status
The output below shows that GIT knows about the new file but it's NOT tracking it yet..
[automation@control test]$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
It's telling us that we need to add the file to the "repo workspace" by running:
git add README.md
Now, our "git status" output is different. GIT is now telling us that it has NO COMMITS but it's now tracking the README.md file and its changes.
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
Now we need to commit our changes to our repo. A "commit" is basically a snapshot of your current changes to the files that you have been tracking (via git add).
git commit -m "new repo first commit"
With the above commit command, we're saying "of the changes to the files GIT is tracking in this directory/repo" we saved a "snapshot" of it all and have made a checksum of those changes. This is now a point-in-time that we ca neither revert to later or push to the upstream github repo we created.
That's nice but how do I associate my local repo with a remote repo...see below.
git remote add origin https://github.com/jsanderson104/test.git
There we have it - the local repo and the remote repo are now linked.
Next, since we know that GitHub uses the default "main" branch (instead of the controversial "master" branch name...sigh). We need to make a new branch on our local repo and move everything to it instead of the "master" branch that our local GIT repo is using. Let's do that with:
git branch -M main
Now we are ready to push to an empty pre-existing repo in GitHUB...
git push --set-upstream origin main
We're done.
Notice how the push command only asks for credentials and already knew what URL to use... That is what the "git remote add origin" command above does for us. It links the local repo to the remote repo.