Skip to content Skip to sidebar Skip to footer

Git Upload to a Remote Repository Terminal

If you are a starter to Git, you must have thought how you would create a Git repository and delve into the features Git provides to make your source code management efficient and flawless no matter what the size of your team is.

The answer is, y'all make up one's mind how you commencement. In this post, it's essential to introduce the 3 main workflows that guide you to start with creating your Git local and remote repositories.

After learning more than about the workflows, you lot'll be able to decide which one that best suits your needs.

In this mail, the three primary workflows of Git repositories creation volition be explained by Git commands only using Git bash on Windows.

To create local Git repositories and know how to connect them to the remote ones y'all should know the post-obit:

  1. Prerequisites to starting with Git repositories.
  2. The four stages of Git.
  3. The three workflows to create a Git repository.
  4. Create a new blank projection with Git.
  5. How to add Git to an existing project source code.
  6. Joining an existing project on GitHub (Cloning a Git repository).

Prerequisites to starting with Git repositories

Before diving into Git, it's important to mention that you lot need to take Git bash control prompt adequately installed and configured on your Windows machine and it responds correctly to Git commands without errors and you have the necessary noesis near bash commands.

Whether you installed Git bash or not, nosotros recommend that you check the Easiest fashion to install Git bash commands on Windows and make sure you have all the right installations and configurations gear up.

The four stages of Git

In this department, we found that it's crucial to start past introducing the necessary data about the four stages of Git in which volition testify how you can add a file to a Git repository and the steps it passes through.

Most of the online resources depict this as the three stages of Git, but nosotros found that it's more appropriate to describe them as four stages. The following will explain why:

Phase ane: The working directory

The working directory is mainly the directory that holds all your projection files on your calculator. In this stage, changes may or may not be tracked and managed by Git.

Stage ii: The staging area

You can refer to this stage as "Git index", and it's as simple as an intermediate area that queues upward your changes in one place for the side by side commit. Files in the staging expanse are tracked and managed by Git.

Phase 3: The Git repository (or the commit history)

In this step, Git creates the local repository and adds the ".git" folder in the project folder structure, and it contains the actual Git repository. Files that are committed in the staging area are moved to the Git repository and added to the commit history.

The in a higher place three stages represent your local Git repository. In other words, until step three, no files have been uploaded to the remote repository at GitHub (or any split up remote repository) yet and here comes the fourth stage.

Stage 4: The remote repository

The fourth stage is the last footstep in a Git workflow, and information technology's basically where the code will be pushed (uploaded) to the remote repository on the internet. The remote repository has all the previous iii stages internally.

Three Stages of Git Life Cycle

The 3 workflows to create a Git repository

The three workflows of Git is the right identify to get started. At whatsoever particular point in time you will match one of the following workflows:

  1. Create a blank project: You lot take not written whatever lawmaking or started your project nonetheless, merely you want to create a new empty project using Git.
  2. Add Git to an existing projection: You are either in progress or finished your project, and you have some electric current source code locally on your file system that y'all want to add to a Git local and remote repository and go along your project using Git.
  3. Join an existing remote project: At that place is a current running project with source code on a remote repository on GitHub and you want to join this project by cloning (downloading) the source code to a local Git repository on your machine.

The following sections will explain each workflow in details and with Git bash commands.

Create a new blank project with Git

In this section, we'll explain the steps required to create a new project with Git.

Step 1: Browsing to the right path

  1. Create a new binder in your Windows file explorer. We will presume that the address to this binder is "C:\Projects\Git" where you will create your Git projects.
  2. Open Git bash and type in the following command to browse to the folder created

The double quotes are mandatory despite no spaces in the path. Now your Git bash should show the electric current path equally shown in the screenshot beneath

git browse to the projects folder path

Stride 2: Create the new project using git init command

Let'due south assume the project name yous desire to create is "Kaizen". To create a new Git project with that name type in the following control

Later running the previous command, Git bash should display a response like shown in the below screenshot

git init new project command

Now Git created a new empty repository as shown in the previous screenshot. Let'south browse to that created project directory using the following command:

At present Git should prove us that it stepped into the "kaizen" project folder and information technology's continued to the "master" co-operative as shown in the screenshot below

Git created a new project on master branch

The master branch is your main default branch in Git where your concluding commit is. Now allow'due south have a look at the files that Git created by default in the working directory:

ls means to listing the files in that directory while -al means to show all files including the .dot files and folders like .git. The ls command is not a Git command it's rather merely a fustigate command.

bash command to list all files

The .git binder presence means that Git successfully created our local repository and created the .git folder for us that holds all the necessary data for that repository including the commit history. We can too ask Git almost the status of the created repository by running the following control:

Past running this command, Git volition respond every bit shown in the screenshot beneath:

Git status command on the current repository

Stride three: Staging and committing new files

At this level, the previously created Git repository is ready to add any brand-new file to the project. Allow's add together a minimal HTML folio to the working directory "kaizen".

The minimal HTML page file is attached to this mail in the attachments section at the bottom. You can download the minimal HTML file from the post attachments and put information technology in the working directory at the same level of the .git folder.

New minimal HTML page location in Git repository

Now by running the git status control once again, nosotros will find that Git is enlightened of an untracked file (the get-go stage) that you lot should phase to exist committed in the future.

Git new untracked file

Run the following command to add this untracked file to the staging area:

then by rerunning git status, we find that Git has staged the file (the 2d phase).


Git added the file to the staging area

At this step, the file has been staged and ready to exist committed to the local Git repository (the third phase). The staged minimal.html file can be committed by running the following command:

-yard means the commit message that will be saved in the commit history to describe what will be committed. If you lot figured out that yous want to change the commit bulletin afterwards information technology has been committed then type in the following amend control:

Then you will exist prompt to change the commit message, and when you lot finish editing the message you lot can quit by typing: : w q. One time you finish, quit the message editor. You can view history by typing in the following control:

The previous control will view your Git commit history with the following arguments:

  1. The --graph statement will show a graphical wait that will be more obvious with branching and merging.
  2. --all argument means show all information.
  3. The --oneline means show one commit per line.

The history volition exist displayed as shown in the following screenshot.

Git commit history

Step iv: Pushing the local commits to the remote repository on GitHub

At this step, we have our commits saved at the local Git repository. Now information technology'south fourth dimension to create a remote public repository at GitHub and push the changes to it. The steps needed to create a public repository at GitHub are straightforward:

  1. Go to https://github.com, log in with your credentials or create a new business relationship then press the "New Repository" dark-green button.
  2. The "New Repository" greenish button will open up a new page on GitHub where y'all volition exist able to create a new repository. To create a public repository, make your form pick equally shown in the following screenshot so press the "Create repository" light-green button.

GitHub new public repository form

3. GitHub will take you to the master repository page where you should copy the repository URL to the clipboard every bit shown in the following screenshot.

Copy repository URL to clipboard

Now let's get back to Git fustigate on our local organisation and push the local commits in our history to the remote repository we have only created and copied its URL (the quaternary stage). On Git bash, type in the following commands to connect your local repository to the remote one:

The first command will add together an origin (a remote repository) to your local Git and the 2d one will verify the added repository URL. Side by side, it'south time to check and pull all the changes on the remote repository to the local one to go them both aligned. Now merge both local and remote histories by running the post-obit command:

The previous command will get the latest commit history on GitHub and merge it with the latest local commit history on your local Git repository with the following arguments:

  1. origin means your remote repository.
  2. master ways your latest local commit in the local Git repository.
  3. --let-unrelated-histories is to forcefulness merging the remote history with the local one to get both histories aligned and ready for pushing the local commits to the remote repository. --allow-unrelated-histories is used simply in the commencement pull attempt.

Git will prompt for your GitHub credentials then it volition testify a response that looks like the following screenshot.

Git pull and merge unrelated histories

Information technology'due south always recommended to follow the best practice to pull the remote changes/commits first before pushing yours. If you are working with a team, then your local repository is most likely not updated with the latest remote changes.

The last step is pushing the local commits in the local Git repository to the remote one on GitHub by applying the following push command:

Which volition result in a response that looks like the following screenshot:

Git push origin master command

Yous tin can also view the commit history at present to take a visual look for what happened so far on Git by typing in the post-obit Git log command:

Git will testify a history visual response that should look similar the post-obit screenshot.

Git history after push to remote

At this step, you accept a fully working Git local repository that's connected to a remote one, and you will only need to pull remote commits and push your local ones on any subsequent changes.

How to add together Git to an existing project source code

Connecting Git to an existing source code is very similar to what we described before equally initiating a new project from scratch with Git.

The differences are that to add a project with multiple source code files y'all would need a different Git add together command from the previous ane that adds only a single file.

To setup a demo project we added a cipher file to the post attachments section so you tin can download and unzip to a new local directory chosen "kaizen".

If you created this directory in the previous section, then you should delete information technology to start fresh with this new approach.

After you lot terminate unzipping the file to your local directory that'southward chosen "kaizen" let's browse to this directory using Git bash assuming that the working directory "kaizen" is placed in the "C:\projects\git" path. On Git bash type in the following command:

One time Git'due south default path is changed to that directory, let's initiate a git project by running the following command:

Notice that there are no arguments beside the init command which is unlike from the start git init command that nosotros ran before that had a project name as an statement.

Git should respond that it created a new empty repository and you should exist able to run across that it's currently set to the "main" co-operative as shown in the previous screenshots.

Now if we ran the git status command we would observe that all the contents of the "kaizen" working directory are untracked as shown in the following screenshot.

Git status untracked files

At this pace we should add all those untracked files to the staging surface area by running the following command:

Note the . (dot) argument that means that Git should add all files and folders recursively rather than adding unmarried files.

Once you run the previous control, follow the following steps to commit your changes to the local Git repository then push information technology to the remote one on GitHub as shown in details in the previous workflow:

  1. Type in the git status command equally shown earlier to bank check if all files and folders take been added successfully.
  2. Type in the git commit with -g argument to commit your changes to the local Git repository as shown earlier.
  3. As shown before, go to GitHub to create a new repository if you don't accept a remote repository on GitHub yet.
  4. Connect the local Git repository with the remote one using the git add together origin command.
  5. Before y'all push your changes to the remote repository, it's always recommended to practice a git pull with forcing a merge to the unrelated histories as shown before.
  6. In the final step, type in the git push button origin master control to push button your local commits to the remote repository on GitHub.

At this step, it's useful to add together some commands to the procedure. To undo the added files to the staging area, y'all tin run the following commands:

At any point of time you tin can also run the following Git command to view all tracked files past Git:

Joining an existing project on GitHub (Cloning a Git repository)

In this workflow, you joined a new team, and you are given a GitHub remote repository and asked to bring together the project placed in this repository.

This workflow is likewise very like to the previous ones except that you will need to clone the remote repository to your local arrangement as the outset pace and so subsequently cloning the remote repository successfully, you will do the aforementioned as described before to add, pull, button or remove files.

To clone a remote repository, go to where the remote repository is on GitHub and copy the GitHub repository URL equally shown before in the first workflow in this mail service.

We will presume that the remote repository is our "Kaizen" demo project that we have been working on in this mail for the demo purposes. To clone this repository, type in the post-obit Git command:

Git volition create the project binder by default by running the in a higher place command.

Later cloning the remote repository to a local one, you will be able to add, edit or remove files, track them by calculation them to the staging area, committing them to the local Git repository and finally pushing them to the remote 1 on GitHub.

What You Should Read Next

The following articles are related to three ways to create git local and remote repositories.

The ultimate guide to the most common 3 ways to rename, change or delete Git branches locally and remotely using Git bash, GitHub and Visual Studio

The definitive guide to remove Git files from tracking, staging area, commit history and the remote repository after push button.

The easiest guide to Git Fustigate for windows download and, the bones configurations needed to install Git bash commands to manage your repositories

lindseymulend72.blogspot.com

Source: https://www.sitereq.com/post/3-ways-to-create-git-local-and-remote-repositories

Postar um comentário for "Git Upload to a Remote Repository Terminal"