DEV Community

Cover image for Using Git and Github - Basic Functions (Easy)
Vlyth_r
Vlyth_r

Posted on • Edited on

Using Git and Github - Basic Functions (Easy)

The purpose of this article is to guide you through some basic functions of Git in the order you'll need to use them with github.

Cloning a Repository

Cloning a repository allows you to create a local copy of a remote repository from GitHub on your computer. This enables you to work with the repository's files, make changes, and contribute to the project. By cloning, you can access the codebase and collaborate with others without directly modifying the original repository. You need the repository's URL to clone it. GitHub provides a convenient "Code" button that offers the repository URL for you to copy. Here's how you do it:

  • Open your web browser and go to https://github.com.
  • Sign up for a GitHub account if you don't already have one. Follow the instructions provided to create your account.
  • Once you're signed in, find the repository you want to clone. You can search for repositories using the search bar at the top of the GitHub page.
  • Click on the repository name to open its main page.
  • On the right-hand side of the repository's main page, you'll find a green "Code" button. Click on it.
  • A dropdown will appear. Click on the clipboard icon to copy the repository's URL.
  • Open the Terminal on your Linux computer.
  • Navigate to the directory where you want to clone the repository. You can use the cd command followed by the directory path.
  • In the Terminal, enter the following command, replacing <repository-url> with the URL you copied from GitHub:
git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode
  • Press Enter to execute the command.
  • Git will start cloning the repository, creating a local copy on your computer.

Making Changes

After cloning the repository, you can make modifications to the files within it. This step involves using a text editor or IDE to add, modify, or delete files according to your requirements. You can write new code, fix bugs, update documentation, or perform any other necessary changes:

  • Use a text editor or Integrated Development Environment (IDE) to make changes to the files within the repository.
  • You can add, modify, or delete files according to your requirements.
  • Save the changes after making modifications.

Checking Status

Git keeps track of changes made to files in your local repository. The "git status" command allows you to see the current status of your local repository compared to the remote repository on GitHub. Checking the status helps you identify which files have been modified, which files are untracked (newly created), or which files have been deleted.
This step provides an overview of the changes you've made and helps you ensure you're ready to proceed to the next steps. To check the status:

  • In the Terminal, navigate to the directory of the cloned repository. You can use the cd command followed by the repository's directory name.
  • Enter the following command to check the status of your changes:
git status
Enter fullscreen mode Exit fullscreen mode
  • Press Enter to execute the command.
  • Git will display a list of modified files or untracked files.

Staging Changes

To include your changes in the next commit (upload), you need to stage them. Staging allows you to selectively choose which changes you want to include in the next commit.
Staging helps you organize and group related changes together, making your commits more meaningful and organized.
The "git add" command is used to stage changes. By specifying files or using the "." character, you can stage specific files or all the changes in your local repository.

  • Enter the following command to stage all the changes:
git add .
Enter fullscreen mode Exit fullscreen mode
  • If you want to stage specific files, replace . with the file names or paths.

Commiting Changes

Committing is the act of saving your staged changes to your local Git repository along with a descriptive message explaining the changes. Commits serve as checkpoints in your project's history, allowing you to track and revert changes if needed. Each commit should have a meaningful message that describes the purpose and intent of the changes made in that commit. The "git commit" command is used to create a commit, and the "-m" flag allows you to provide a commit message, so:

  • After staging the changes, you need to commit them with a descriptive message.
  • Enter the following command to commit the changes:
git commit -m "Descriptive commit message"
Enter fullscreen mode Exit fullscreen mode
  • Replace "Descriptive commit message" with a brief summary of the changes you made.
  • Commit messages should be meaningful and describe the purpose of the commit.

Pushing Changes

Pushing allows you to upload your committed changes from your local Git repository to the remote repository on GitHub. Pushing makes your changes visible to others working on the project and enables collaboration. It also ensures that your changes are backed up and accessible from anywhere. The "git push" command is used to push your committed changes to the remote repository on GitHub.
You will need to provide your GitHub username and password to authenticate and authorize the push. Summarizing:

  • Once you have committed your changes, you can push them to the remote repository on GitHub.
  • Enter the following command to push your changes:
git push
Enter fullscreen mode Exit fullscreen mode
  • Git will prompt you to enter your GitHub username and password.
  • Type your credentials and press Enter. Note: When typing your password, you won't see any characters on the screen for security reasons.
  • Git will upload your changes to the remote repository. That's it! You have successfully used the basic Git functions. You can now clone repositories, make changes, commit them, and push them to GitHub. Remember to navigate to the correct directory in the Terminal for each repository and replace <repository-url> with the actual URL of the repository you want to clone.

Top comments (0)