You are here:

How to Use Git to Create, Push, and Pull Local Repository to Remote

How to Use Git to Create, Push, and Pull Local Repository to Remote

Overview

In 2005, Linus Torvalds created Git, and since then, it has been actively managed by Junio Hamano, a programmer from Japan. Git stands out as one of the most widely used version control systems, with millions of projects worldwide, both commercial and open source, relying on Git for efficient version control.

Key advantages of using Git include:

  1. Distributed Version Control: Git adopts a peer-to-peer approach, in contrast to other systems like Subversion (SVN) that use a client-server model.
  2. Independent and Massive Code Branches: Developers can work on branches of code that are both independent and substantial.
  3. Efficient Branch Management: Creating, deleting, and merging branches is faster and smoother.
  4. Cryptographic Integrity: Git employs data models to ensure cryptographic integrity within the repository, generating a checksum for every added or committed file.
  5. Staging Area: Git introduces a staging area or index, allowing developers to format and review commits before implementation.

Git is known for its simplicity. To begin, users can create a new repository or checkout. After installation, the ‘git init’ command sets up everything. Additionally, ‘git clone’ creates a copy of the local repository for the user.

Requirements

These are the prerequisites before you start the tutorial:

  1. Set up a host for Git in your local environment, or you can use a VPS server [1].
  2. Install an SSH client, which you can download [2].
  3. Ensure you have credentials for accessing the server using the SSH client.

Installation

The simplest method to install Git on Linux is by executing the following command:

  • For Debian / Ubuntu
root@host-ubuntu:~# apt-get install git
  • For Fedora / CentOS / Scientific
root@host-centos:~# yum install git

For Windows, download Git [3], open the file, and follow the installation process. Click ‘Next’ and ‘Finish’ following all instructions until the installation is complete.

Run the following command to verify the successful installation:

root@host-ubuntu:~# git --version

Execute the following command in your terminal to configure the email and username to be used for your Git. Ensure that the username and email match your GitHub account. If you don’t have a GitHub account, you can create one [4].

root@host-ubuntu:~# git config --global user.name "hernantaa"
root@host-ubuntu:~# git config --global user.email "hernantaa@gmail.com"

Verify the configuration using the following command:

root@host-ubuntu:~# git config --list
Install and Configure Git

Set Up a GIT Repository

You can initialize a Git project using one of two main approaches. The first is to take an existing project or directory and put it under Git. The second is to clone/duplicate an existing Git repository from the server. In this tutorial, we will create a new repository.

Some options for the ‘git init’ command include:

OptionsFunction
-q, –quietDisplay only error and warning messages; other output is suppressed.
–bareRepositories created with the –bare option are referred to as bare repositories.
–template=Specify the directory for templates to be used.
–separate-git-dir=By default, the .git directory is stored in the same location as the working tree. This option separates the .git folder from the working tree.
–shared[=(value)]Facilitates the creation of a shared Git repository among multiple users, allowing users in the same group to push into the shared repository.

The option can take on the following values, with the default being ‘group’ if no value is provided:

OptionsFunction
umask (or false)Default permissions will be set based on the umask if the –shared option is not specified.
group (or true)If you create a group-writable repository for all developers, it implies the repository is writable for the group. Change the umask to 002 for all developers so that new files can be created with group-writable permissions.
all (or world or everybody)Make the Git repository readable for all users.
0xxx0xxx is an octal number used to set permissions for user and group access. For example, if you set permissions to 0640, it will create a repository only readable by the group, not writable or accessible to others. However, if you set permissions to 0660, it will create a repository readable and writable by the current user and current group but inaccessible to others.

To begin, create a directory for your Git project.

root@host-ubuntu:~# mkdir git-tutorial
root@host-ubuntu:~# cd git-tutorial/

The subsequent command will set up a repository in the current directory

root@host-ubuntu:~/git-tutorial# git init .

Git will generate a new subdirectory named .git, which contains all the essential files for your repository – the Git repository framework. At this stage, nothing from your project has been tracked.

Create a project directory, run 'git init' to set up a Git repository.

Basic Function

  1. git status
  • Execute the ‘git status’ command to view the current state of your project.
root@host-ubuntu:~/git-tutorial# git status
  • Generated a file named test.txt within the git-tutorial repository.
root@host-ubuntu:~/git-tutorial# touch test.txt
  • Run the ‘git status’ command again to observe the changes in the repository status:
root@host-ubuntu:~/git-tutorial# git status
Check Git Project Status

The ‘git status’ indicates that test.txt is ‘untracked.’ This signifies that Git recognizes test.txt as a new file. To instruct Git to commence tracking changes made to test.txt, you must add it to the staging area using the ‘git add’ command.

  1. git add
  • Enter the ‘git add’ command to stage test.txt.
root@host-ubuntu:~/git-tutorial# git add test.txt
  • Execute ‘git status’ again to review the changes.
root@host-ubuntu:~/git-tutorial# git status
Use 'git add' to stage changes and 'git commit' to commit changes to your Git repository

Now the test.txt file is in the staging area, but it hasn’t been added to our repository yet. You can add or remove files from the stage before storing them in the repository. To commit the files from the staging area to the repository, use the ‘git commit’ command.

  1. git commit
  • To commit the files from the staging area to the repository, execute the ‘git commit’ command.
root@host-ubuntu:~/git-tutorial# git commit -m "create test.txt"
Use 'git commit -m' to commit changes to Git repository

Congratulations! You have successfully added the test.txt file to the repository.

  1. git log
  • To view the history after adding and committing the test.txt file to the repository, you can use the ‘git log’ command.
  • Run ‘git log’ to see the log.
root@host-ubuntu:~/git-tutorial# git log
Use 'git log' to view the commit history in your Git repository

Push Local Repository to The Remote Repository (GitHub)

  1. First, before pushing your local repository, create a repository on GitHub.
  • Log in to GitHub and create a new repository.
Log in to GitHub, create a new repository, and push local repository to the remote repository
  • Enter the repository name and click ‘Create repository.’
Input the repository name and click 'Create repository

Now, you have successfully created a new repository on GitHub named ‘git-tutorial.’

  • Remember to copy your repository URL.
New GitHub repository 'git-tutorial' created successfully
  1. To push the local repository to the GitHub server, it’s essential to add a remote repository.
  • Proceed by executing ‘git remote add’ with the following options:
root@host-ubuntu:~/git-tutorial# git remote add origin https://github.com/hernantaa/git-tutorial.git
  • Push your local repository to GitHub by running the following command, then enter your username and password on GitHub.
root@host-ubuntu:~/git-tutorial# git push -u origin master
Add 'origin' as the remote repository and push to GitHub

The name of the remote is ‘origin,’ and the default local branch name is ‘master.’ The ‘-u’ flag instructs Git to remember the parameters, so that next time we can simply run ‘git push,’ and Git will know what to do.

    • Verify the result on your GitHub repository.
Check your GitHub repository to confirm the successful cloning of 'git-tutorial' to your local machine.

Cloning a Repository from GitHub to Local Machine

After pushing the local repository to GitHub, if you wish to create a copy of an existing Git repository, use the ‘git clone’ command.

Clone the git-tutorial repository from your GitHub repository to the new host by running the following command:

root@newhost-ubuntu:~# git clone https://github.com/hernantaa/git-tutorial.git

Inspect the cloned repository, and you will find the test.txt file.

root@newhost-ubuntu:~# ls git-tutorial
Use command to copy the 'git-tutorial' repository from GitHub to the local machine

This concludes the tutorial. I hope you have gained a good understanding of Git. If you encounter any bugs or documentation errors, please leave your comments below.

 

[1]: https://www.blendhosting.com/
[2]: https://the.earth.li/~sgtatham/putty/latest/w32/putty.exe
[3]: https://git-scm.com/download/win
[4]: https://github.com/

Was this article helpful?
Dislike 0