How to Setup Local Repo and Push to Remote Repository?

In this blog post we will learn from creating a local repository in your local machine to push the changes to remote repository.

Prerequsitie:

Git Should be installed and Path should be configured
Java Should be installed and path should be configured

Follow the Steps:

Create a Java Project / Maven Project in your Local machine
How to Setup Local Repo and Push to Remote Repository? 1
Goto the Project Folder and Convert that project into Local repository by executing git init Command
How to Setup Local Repo and Push to Remote Repository? 2
How to Setup Local Repo and Push to Remote Repository? 3
Add a File into your Project in Eclipse
01. Hello World Program
Check the File Status in Git by executing the command Git Status
How to Setup Local Repo and Push to Remote Repository? 4

Download .gitignore file

After Adding .gitignore file

How to Setup Local Repo and Push to Remote Repository? 5
If the File is not added then add that file by executing git add * or git add . command
How to Setup Local Repo and Push to Remote Repository? 6
Check the file status after adding the files into staging area
How to Setup Local Repo and Push to Remote Repository? 7
Once the files are added into git, commit the changes by executing the command
git commit -m “Message”
How to Setup Local Repo and Push to Remote Repository? 8
After commit the changes the next step is push the changes to remote repository. So to push the changes the remote repo you can execute the command
> git push -u origin master
How to Setup Local Repo and Push to Remote Repository? 9

This will give you error because we have not linked our local repository with the remote repository.

So to add remote repository with local repository, there should be a remote repository available in GitHub website with the same name as your project name in local.
How to Setup Local Repo and Push to Remote Repository? 10
Get the remote repository URL and execute the below command
> git remote add origin <repo url>
How to Setup Local Repo and Push to Remote Repository? 11
Now local repository is linked with the remote repository. So lets try to push the changes to remote repository.
How to Setup Local Repo and Push to Remote Repository? 12

When you try to push the changes, it will ask for the authentication. You have to authenticate with the Personal access token or you can sign in with your browser.

Now you can see the successful message like below.

How to Setup Local Repo and Push to Remote Repository? 13

You can see the changes in the remote repo as well.

How to Setup Local Repo and Push to Remote Repository? 14

Create a New Branch and Push to Repository

Check all branches available in command prompt with the below command
> git branch
How to Setup Local Repo and Push to Remote Repository? 15
Create a new branch and switch to the newly created branch.
> git checkout -b <new branch name>
How to Setup Local Repo and Push to Remote Repository? 16
Add a New file in your Java project and delete the classes in the project folder.
Note: If you have added the git ignore file then this step is not required.
How to Setup Local Repo and Push to Remote Repository? 17
After adding the new file run the git status command to check the status of file newly added files.
How to Setup Local Repo and Push to Remote Repository? 18
To add the new file into staging, execute the below command
> git add . or git add *
How to Setup Local Repo and Push to Remote Repository? 19
After adding all the files, checking the status again
> git status
How to Setup Local Repo and Push to Remote Repository? 20
After adding all the files, commit the changes by executing the below command
> git commit -m “Message”
How to Setup Local Repo and Push to Remote Repository? 21
Push the changes to remote with executing the below command
> git push -u origin <new branch name>

You will get the error

So lets try to push again
> git push

Again You will get an fatal error with a message that remote repository does not have the same branch name as local repository.

So to push the newly created branch of local repository to remote repository execute the below command.
> git push –set-upstream origin <new branch name>
How to Setup Local Repo and Push to Remote Repository? 22
Finally a new Branch will be created in the remote repository with the same name as local repository.
How to Setup Local Repo and Push to Remote Repository? 23

Complete Pull & Merge Request

After the second commit you can see there are two branches created in remote repo and also you can see the compare & pull request button as well.
How to Setup Local Repo and Push to Remote Repository? 24
Two merge the changes of secondcommit branch to the master branch click on the Compare & Pull Request button.
How to Setup Local Repo and Push to Remote Repository? 25
While Creating the pull request you have to choose from which branch you want to merge the code and along with you need to assign the reviewers if there is someone.
How to Setup Local Repo and Push to Remote Repository? 26
To merge the changes into the master you have to click on the merge pull request button.
How to Setup Local Repo and Push to Remote Repository? 27
Finally you need to confirm by clicking on the confirm merge button.
How to Setup Local Repo and Push to Remote Repository? 28
Finally the Full request is merged into master branch and you can check the master branch for the confirmation.
How to Setup Local Repo and Push to Remote Repository? 29

Categories GIT
Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment