• Ubuntu
  • Python
  • Windows
  • VirtualBox
  • Git
  • Docker
  • Windows
Chat
Writy.
No Result
View All Result
My Account
Writy.
No Result
View All Result
sxi
No Result
View All Result

Home Git – Push Changes to Remote Git Branch

Git – Push Changes to Remote Git Branch

SXI ADMIN by SXI ADMIN
February 13, 2020
in Git
Reading Time: 10 mins read
A A
0
Share on FacebookShare on Twitter
In this article, I am going to show you how to push (upload) your local Git repository to a remote Git repository hosted on Git cloud services such as GitHub/BitBucket etc. I will also show you how to push the changes that you’ve made on your local Git repository to the remote Git repository. So, let’s get started.

Creating a Test Local Git Repository:

You will most likely have a local Git repository by now, if you’re reading this article. But, if you don’t have one yet and you want to follow along, then you can easily create a local Git repository as follows:

$ git init my-project

A new Git repository my-project is created.

Now, navigate to the my-project/ directory as follows:

$ cd my-project/

Now, I am going to create a dummy index.html file as follows:

$ echo ‘<h1>Hello World</h1>’ > index.html

As you can see, a new file index.html is created.

Now, stage the file as follows:

$ git add .

Make a new commit as follows:

$ git commit -m ‘initial commit’

A new commit initial commit is created.

The new commit should appear in git log as you can see in the screenshot below.

$ git log –oneline

Creating an Empty GitHub Remote Git Repository:

Now, you have to create an empty remote GitHub repository on any Git cloud service you want. If you’re using GitHub as I am, then, login to your GitHub account. Now, to create a new GitHub repository, click on the + icon and then click on New repository.

Now, type in a name for the new repository. If you want, type in a description (optional). Select Public or Private depending on whether you want to set the repository public or private. Now, make sure Initialize this repository with a README is not checked. Finally, click on Create repository.

An empty GitHub repository should be created. This is our remote Git repository. The repository URL is given below as you can see in the marked section of the screenshot below.

Adding Remote Repository Information to Local Git Repository:

Now, copy the URL of the GitHub repository. You can do so by clicking on the  icon as marked in the screenshot below.

Now, you have to add the GitHub URL to your local repository. To do that, run the following command:

$ git remote add origin https://github.com/dev-shovon/my-project.git

Here, origin is the name or alias of the GitHub repository URL that you’ve just added. You will have to use the alias whenever you push your local repository or changes to your local repository to the remote GitHub repository.

You can list all the remote repositories that you’ve added as follows:

$ git remote -v

As you can see, the GitHub repository URL I’ve just added is here. It’s referred to as origin.

Pushing Local Git Repository to Remote Git Repository:

Now, you can push the default branch master of your local Git repository to the remote GitHub repository as follows:

$ git push -u origin master

Here, origin is the alias of the remote Git (GitHub in this case) repository URL and master is the branch that you want to push to the remote Git repository.

NOTE: The -u option is used only for the first push of any branch only. As, I am pushing the local master branch to the GitHub repository for the first time, I’ve added -u option here. This creates a tracking branch for the master branch. The next time you push anything to the master branch, you don’t have to use the -u option again.

You will be asked to provide the username and password of your GitHub account. Once you do, the contents of the default branch master from your local Git repository will be pushed to the GitHub repository.

As you can see, the master branch is uploaded to the empty GitHub repository that I’ve create earlier.

Pushing Local Changes to the Remote Git Repository:

Now, let’s make a simple change to the index.html file on my local Git repository.

As you can see, I’ve modified the index.html file.

$ git status

Now, stage the changes as follows:

$ git add .

Now, commit the changes as follows:

$ git commit -m ‘added a paragraph to the index.html page’

The changes should be commited.

The new commit is listed in git log as you can see in the screenshot below.

$ git log –oneline

Now, push the changes of the local master branch to the GitHub repository as follows:

$ git push origin master

The changes should be pushed to the GitHub repository.

As you can see, the GitHub repository is updated.

Pushing Other Branches to the Remote Git Repository:

In this section, I am going to show you how to push other branches to the GitHub repository and push changes to these branches to the GitHub repository as well.

First, create a new branch, let’s say ie-fix as follows:

$ git branch ie-fix

Now, checkout to the newly created branch ie-fix as follows:

$ git checkout ie-fix

Now, make a little change to the index.html file.

Now, stage the changes as follows:

$ git add .

Now, make a commit as follows:

$ git commit -m ‘fixed ie problem’

Now, push the newly created branch ie-fix to the GitHub repository as follows:

$ git push -u origin ie-fix

The ie-fix branch should be pushed to the GitHub repository.

As you can see, the ie-fix branch is pushed to the GitHub repository.

Now, you can push any changes of the ie-fix branch using the git push command without the -u option.

Make a little change to the index.html file.

Stage the changes and make a commit as follows:

$ git add .
$ git commit -m ‘added a horizontal line to the page’

Now, push the changes of the ie-fix branch to the GitHub repository as follows:

$ git push origin  ie-fix

The changes of the ie-fix branch should be pushed to the GitHub repository.

As you can see, the new commit is displayed in the GitHub repository page.

So, that’s how you push changes to remote Git branches. Thanks for reading this article.

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0
SXI ADMIN

SXI ADMIN

Related Stories

Configure Git Server with SSH on CentOS 8

Configure Git Server with SSH on CentOS 8

by SXI ADMIN
February 13, 2020
0

If you have a small tech company, you can set up an SSH based Git server so that the people...

Configure Git Server with HTTP on CentOS 8

Configure Git Server with HTTP on CentOS 8

by SXI ADMIN
February 13, 2020
0

In this article, I am going to show you how to configure Git Smart HTTP server on CentOS 8 for...

Installing and Getting Started with Git on Debian 10

Installing and Getting Started with Git on Debian 10

by SXI ADMIN
February 13, 2020
0

Git is a very popular Version Control System (VCS). It is used to track changes in source codes during software...

5 Best Self-hosted GitHub Alternatives

5 Best Self-hosted GitHub Alternatives

by SXI ADMIN
February 13, 2020
0

GitHub may be the most popular computer code hosting service for version control using Git, which is a distributed version...

How to Create Branches on Git

How to Create Branches on Git

by SXI ADMIN
February 13, 2020
0

Git is one of the best version control system out there. It is very popular among almost every type of...

How to Delete Remote Branch in Git

How to Delete Remote Branch in Git

by SXI ADMIN
February 13, 2020
0

In this article, I am going to show you how to delete or remove a remote Git branch. I will...

Load More
Next Post
How to use variable in awk command

How to use variable in awk command

Popular Story

  • EMEDITOR PROFESSIONAL Full Key V11~V19

    645 shares
    Share 258 Tweet 161
  • Plesk Obsidian Lifetime license Key Version 18.0.15

    644 shares
    Share 258 Tweet 161
  • Plesk Onyx + Obsidian 18 Web Host Edition Nulled/Crack

    614 shares
    Share 246 Tweet 154
  • How To Start/Stop/Restart Cron Service In Linux Centos 7

    610 shares
    Share 244 Tweet 153
  • Download Windows Templates for Virtualizor KVM Windows server 2008,2012,2016,2019

    608 shares
    Share 243 Tweet 152
SXI.IO

© 2022 Vps giá rẻ

Navigate Site

  • Ubuntu
  • Python
  • Windows
  • VirtualBox
  • Git
  • Docker
  • Windows

Follow Us

No Result
View All Result
  • Ubuntu
  • Python
  • Windows
  • VirtualBox
  • Git
  • Docker
  • Windows

© 2022 Vps giá rẻ