Initialize local git repository, push to the remote repository

In this post, we’ll deal with

  • Initializing the local repository
  • Create the remote repository in Github, bitbucket, Gitlab
  • Add the remote URL for local repository push the initial commit

Cloud-hosted git servers publicly available to create the repositories for free for individuals.

Now let’s initialize the local repository, Create the directory and initialize the directory as the git repository.

mkdir my-first-git-repository && cd "$_"
git init
Output
Initialized empty Git repository in ../my-first-git-repository/.git/

Now git initializes a hidden folder with .git name. File structure looks like

.
├── branches
├── config
├── description
├── FETCH_HEAD
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
9 directories, 17 files

All git actions will be stored in the .git folder of every action we do at a local or remote server. If we need a deep understanding of the git folder structure let me know as a comment so I will create a new post for it.

Now let’s add some file and check with few git commands.

echo "My first change" > firstfile.txt

Now local repository provides the tracking of the file, to look at the changes we did, to check with the status of the git we run

git statusOUTPU
On branch master
No commits yetUntracked files:
(use "git add <file>..." to include in what will be committed)
firstfile.txt
nothing added to commit but untracked files present (use "git add" to track)

Now we’ll add the files to commit with

git add --all   # Add all files in entire repository
# OR
git add . # Add all files in the current directory
# OR
git add firstfile.txt # Add specific file

Now check with the status again

git statusOUTPUT
On branch master
No commits yetChanges to be committed:
(use "git rm --cached <file>..." to unstage)
new file: firstfile.txt

Let’s commit the changes added or staged.

git commit -am "MY FIRST COMMIT"OUTPUT
[master (root-commit) 945c423] MY FIRST COMMIT
1 file changed, 1 insertion(+)
create mode 100644 firstfile.txt

git commit will create a commit ID so that we can move back in time with the commit history to revert to the old version or to understand what is the code change with the commit ID.

The -a flag is used in git to add all the files to your commit and then you’ll have to run another command where you write your commit message. The m flag is used for connecting a commit message to your commit for example `git commit -m “your message”

When to go remote repository

  • If there is more than one contributor
  • If you don’t trust your machine disk, it may get crashed anytime
  • Exchange the code change between the servers for writing the code in IDE in dev machine, for deploy in the production server

With the git init local, we are done with creating the local repository, now we’ll create a remote repository in git host providers. there are many git host providers available on the internet, but few are popular, are Github, Gitlab, Bitbucket.

Now we’ll create a repository in the in GitHub and connect our local repository and push the changes from local server to remote server.

We need to have the account with git host provider to create the repository, here is the screenshot click on Create Repository

Github — Create repository link

Fill the required data and click on create. then you are done.

Github — Create Repository
  • We can import from different git server
  • give the name of the repository — mandatory
  • Description of the repository
  • We need to opt for privet or public based on the project accessibility
  • We can initialize repo with README.md, which is not recommended for the first-timers
  • Add .gitignore if required, not recommended for first-timers
  • Add LICENCE if required, not recommended for first-timers

As we click on the create we navigate to the set of instructions page to connect local repository with remote.

Push existing repository to remote repository
echo "# demo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/JinnaBalu/demo.git
git push -u origin master
> git remote add origin https://github.com/JinnaBalu/demo.git
> git push -u origin master

As we already have the repository in local we can go with the option 2, now let’s get back to our local repository and execute above commands for existing repository

cd my-first-git-repository/
git status
# OUTPU
# On branch master
# nothing to commit, working tree clean
git remote add origin https://github.com/JinnaBalu/demo.git
git push -u origin master
# OUTPUT
# Username for 'https://github.com': JinnaBalu
# Password for 'https://JinnaBalu@github.com':
# Enumerating objects: 3, done.
# Counting objects: 100% (3/3), done.
# Writing objects: 100% (3/3), 247 bytes | 247.00 KiB/s, done.
# Total 3 (delta 0), reused 0 (delta 0)
# To https://github.com/JinnaBalu/demo.git
# * [new branch] master -> master
# Branch 'master' set up to track remote branch 'master' from #'origin'.

Now we can check with the repository whether we got the local changes pushed to the remote. Refresh the page of instruction we got in the prior screen.

Check with the commit ID and cross-check with local commit history

git log# OUTPUT
# commit 945c423086e3b62bc2a504775d7b743ed51079f5 (HEAD -> master, origin/master)
# Author: Jinna Balu <jinna.balu@wisestep.com>
# Date: Thu Jun 18 09:25:26 2020 +0530
# MY FIRST COMMIT

That's it. We are done with creating the first commit to the first repository we created.

Devops Manager, Practising Natural Farming