How to Initialize a Git Repository Again
Initialize local git repository, push to the remote repository
In this post, we'll bargain with
- Initializing the local repository
- Create the remote repository in Github, bitbucket, Gitlab
- Add the remote URL for local repository push button the initial commit
Github, Bitbucket, Gitlab
Cloud-hosted git servers publicly available to create the repositories for costless for individuals.
Git Local Repository
Now allow'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-starting time-git-repository/.git/
Now git initializes a hidden folder with .git proper noun. File construction looks like
.
├── branches
├── config
├── clarification
├── FETCH_HEAD
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── mail-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── set up-commit-msg.sample
│ ├── pre-push button.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags ix directories, 17 files
All git actions volition be stored in the .git folder of every activeness we do at a local or remote server. If we need a deep agreement of the git folder structure allow me know as a comment and so I will create a new post for it.
Now let'due south add together some file and bank check with few git commands.
echo "My offset modify" > firstfile.txt
Now local repository provides the tracking of the file, to look at the changes we did, to bank check with the status of the git we run
git condition OUTPU
On branch principal No commits yet Untracked files:
(use "git add together <file>..." to include in what volition exist committed)
firstfile.txt nothing added to commit just untracked files present (use "git add" to runway)
Now nosotros'll add the files to commit with
git add together --all # Add together all files in entire repository
# OR
git add . # Add all files in the current directory
# OR
git add firstfile.txt # Add specific file
Now bank check with the status again
git status OUTPUT
On branch master No commits yet Changes to exist committed:
(employ "git rm --cached <file>..." to unstage)
new file: firstfile.txt
Allow's commit the changes added or staged.
git commit -am "MY Showtime COMMIT" OUTPUT
[principal (root-commit) 945c423] MY Outset COMMIT
1 file changed, i insertion(+)
create mode 100644 firstfile.txt
git commit will create a commit ID then that we can motility back in time with the commit history to revert to the old version or to empathize 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 so you'll have to run another control where you write your commit message. The 1000 flag is used for connecting a commit message to your commit for example `git commit -grand "your bulletin"
Git Remote Repository
When to get remote repository
- If there is more than one contributor
- If you don't trust your machine deejay, it may get crashed anytime
- Exchange the code alter between the servers for writing the code in IDE in dev machine, for deploy in the production server
With the git init local, nosotros are washed with creating the local repository, now nosotros'll create a remote repository in git host providers. there are many git host providers available on the internet, but few are pop, are Github, Gitlab, Bitbucket.
At present we'll create a repository in the in GitHub and connect our local repository and push the changes from local server to remote server.
Nosotros need to have the account with git host provider to create the repository, here is the screenshot click on Create Repository
Fill the required data and click on create. so you are done.
- We can import from different git server
- requite the name of the repository — mandatory
- Clarification of the repository
- We need to opt for privet or public based on the projection accessibility
- We can initialize repo with README.md, which is non recommended for the kickoff-timers
- Add .gitignore if required, non recommended for starting time-timers
- Add together LICENCE if required, not recommended for first-timers
As we click on the create nosotros navigate to the fix of instructions page to connect local repository with remote.
#Option1 Create a new repository on the command line
echo "# demo" >> README.doctor
git init
git add README.medico
git commit -m "first commit"
git remote add together origin https://github.com/JinnaBalu/demo.git
git push button -u origin master
#Option2 Push an existing repository from the command line
> git remote add together origin https://github.com/JinnaBalu/demo.git
> git push -u origin main
As we already have the repository in local we tin go with the selection 2, now let'southward get back to our local repository and execute above commands for existing repository
cd my-outset-git-repository/
git status # OUTPU
# On co-operative chief
# nothing to commit, working tree make clean git remote add origin https://github.com/JinnaBalu/demo.git
git push button -u origin master # OUTPUT
# Username for 'https://github.com': JinnaBalu
# Password for 'https://JinnaBalu@github.com':
# Enumerating objects: iii, washed.
# Counting objects: 100% (3/3), done.
# Writing objects: 100% (iii/three), 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
# Co-operative 'principal' prepare to track remote co-operative 'primary' from #'origin'.
Now nosotros can check with the repository whether we got the local changes pushed to the remote. Refresh the page of education we got in the prior screen.
Check with the commit ID and cross-check with local commit history
git log # OUTPUT
# commit 945c423086e3b62bc2a504775d7b743ed51079f5 (HEAD -> main, origin/master)
# Author: Jinna Balu <jinna.balu@wisestep.com>
# Date: Thu Jun 18 09:25:26 2020 +0530 # MY Kickoff COMMIT
That'due south information technology. Nosotros are washed with creating the beginning commit to the offset repository we created.
Source: https://jinnabalu.medium.com/initialize-local-git-repository-push-to-the-remote-repository-787f83ff999
0 Response to "How to Initialize a Git Repository Again"
Post a Comment