Git create a local repo and push to github - perrigoh/learner_journal GitHub Wiki

Create A Git Repository From Local Directory (Folder)

Create directory (folder) locally to store the project

Steps:

  1. Change directory to the directory where the new folder is to be created:

    $ cd Google\ Drive/Colab\ Notebooks/

    The prompt will change to from [wmxpg] ~ to [wmxpg] ~/Google Drive/Colab Notebooks

  2. Create directory (folder) and change directory to the newly created directory (folder):

    $ mkdir -p -v classify_dog_pytorch_udacity && cd $_

    Output:

    mkdir: created directory 'classify_dog_pytorch'
    

    note: do not leave a space between file name as it will create two folders instead of one.

    info source
    -v or –verbose: It displays a message for every directory created.
    -p: A flag which enables the command to create parent directories as necessary. If the directories exist, no error is specified. Meaning if folder name is new, it will create the folder. If folder name exist in the directory, it will not create the folder and will not prompt any error message.

    info source
    $_: execute the last command, in this case is change directory to the doc folder

Initialise the newly created directory (folder)

Initialise the setup of a repository:

$ git init

Output:

Initialized empty Git repository in C:/...Google Drive/Colab Notebooks/classify_dog_pytorch/.git/

Check status:

$ git status

Output:

On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Create / add some files

Create an empty README file:

$ Echo > README.md

Output:

no output

Add .gitignore file

Gitignore file store files that do not need to be track, especially those that auto-generatedtre by the system. Create a .gitignore file

$ echo > .gitignore

Output:

no output

Though the prompt returned no output, the file was already created. To check:

$ ls -a

Output:

./  ../  .git/  .gitignore  README.md

Specific files do not need to be track

Use Toptal | gitignore.io to generate list of files not to be track base on Operation Systems, IDEs or Programming Languages. Copy and paste those that are relevant into .gitignore.

Specific folder not to be track

Add folder_name (directory_name) with forward slash /assets/

info source, this website gives examples to ignore files, folders and make exceptions.

Stage the file

To add all files use git add . or to add specify file git add filename.
For vs code, click on the + sign beside the file name in the source control side panel.

$ git add .

Output:

warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it

This warning is can be ignored.
Check status:

$ git status

Output:

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   README.md

Commit the changes

$ git commit

Output:

hint: Waiting for your editor to close the file...

VS code editor pop up, type commit message, save and closed:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch add-torcheck
# Changes to be committed:
#	new file:   README.md
#

Output after VS editor is closed:

[main (root-commit) cd7d4dc] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

For vs code, type the commit message in the placeholder then click on the commit button in the source control side panel.

Send commit to remote repository

The origin refers to remote repository and main is the branch in the remote repository.

For the first time using push, add flag -u to set the up stream. This is to link up the remote repository with the local repository for all future pushes.

$ git push -u origin main

Output:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 228 bytes | 228.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/perrigoh/learner_journal.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

All subsequent pushes, flag -u not required.

$ git push origin main

or

Use vs code for this is easier. After click on Publish Branch button, select public or private repository.
Check status:

$ git status

Output:

On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Now this repository is ready.

⚠️ **GitHub.com Fallback** ⚠️