DEV Community

Cover image for A Comprehensive Guide to Git: Understanding Basic Concepts and Essential Commands
Bello Osagie
Bello Osagie

Posted on • Edited on • Originally published at blog.techstackmedia.com

A Comprehensive Guide to Git: Understanding Basic Concepts and Essential Commands

GitHub Logo

Introduction

Git has revolutionized the way developers collaborate and manage their code. Whether you're a beginner or an experienced developer, understanding the basic concepts and essential commands of Git is crucial for efficient version control. In this blog post, we'll explore the fundamental concepts of Git, including push, merge, branches, and more. We'll also provide code snippets to illustrate how these commands are used in practice. Let's dive in!

staging, committing, checking out in git

Installation

For detailed instructions on installing Git, please refer to the official Git website. However, if you're a Windows user, I'll provide you with a few commands to help you install Git.

Steps

To install Chocolatey and use it to install Git, follow these steps:

  1. Search for PowerShell and run it as an administrator on Windows.
  2. Go to the Chocolatey installation page.
  3. If this is your first time installing Chocolatey on your operating system, execute the following command in your terminal, based on the instructions provided on the installation page:
Get-ExecutionPolicy
Enter fullscreen mode Exit fullscreen mode
  • If the output is "Restricted", enter one of the commands below:
Set-ExecutionPolicy AllSigned
Enter fullscreen mode Exit fullscreen mode

or

Set-ExecutionPolicy Bypass -Scope Process
Enter fullscreen mode Exit fullscreen mode
  • Regardless of whether "Restricted" was the previous output or not, run the following command (copy and paste it into your terminal):
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Enter fullscreen mode Exit fullscreen mode
  • If there are no errors, check if Chocolatey was successfully installed by running the following command:
choco
Enter fullscreen mode Exit fullscreen mode

If the output displays the version of Chocolatey you are using, it means the installation was successful.

  • Now, search for the package you need (in this case, Git) by visiting the package search page. Once you find the Git package, execute the following command:
choco install git
Enter fullscreen mode Exit fullscreen mode

Basic Concepts

Before we delve into the commands, let's quickly review some basic concepts of Git:

ls -la

The ls -la command (or ls on Windows) lists all files and directories, including hidden ones, in the current directory. It helps you get a comprehensive view of the files present.

git commit -m "commit msg" -m "desc. msg"

The git commit command records change to the repository. By specifying a commit message (-m flag), you provide a concise description of the changes made. It's good practice to include both a short commit message and a more detailed description.

Generating SSH Keys

You can generate SSH keys to securely connect with remote repositories, such as GitHub. Here's a step-by-step guide:

Generate the SSH key

Use the ssh-keygen command to generate a new SSH key. For example:

ssh-keygen -t rsa -b 4096 -C "email@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Its output will look like below:

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/TechstackMedia - Osagie/.ssh/id_rsa):
Enter fullscreen mode Exit fullscreen mode

Where it asks you to enter a file name, you can enter any file name of your choice but for the case of this tutorial enter testkey and press Enter or Return.

Now you will probably get another question as shown below:

Enter passphrase (empty for no passphrase):
Enter fullscreen mode Exit fullscreen mode

It is asking you to provide a passphrase for encryption or decryption purposes. It is used to protect sensitive data, such as private keys or encrypted files.

You may click enter meaning empty for no passphase but it is generally recommended to set a strong passphrase to enhance the security of your sensitive data.

Locate the key file

After generating the key, you can find it using the ls command combined with grep. For instance:

ls | grep testkey
Enter fullscreen mode Exit fullscreen mode

Here, testkey is the file name containing the SSH key.

Note the testkey without the .pub, its content should not be shared with anyone because it is the key you use to interact with GitHub.

Copy the public key

The public key (with a .pub extension) is the key you'll upload to your GitHub interface. Use the following command to copy it to your clipboard:

Now in the terminal run:

type testkey.pub | clip
Enter fullscreen mode Exit fullscreen mode

Or

cat testkey.pub # copy the entire public key
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can simply run the command below:

pbcopy < testkey.pub
Enter fullscreen mode Exit fullscreen mode

You might get the error shown below:

'pbcopy' is not recognized as an internal or external command,
operable program or batch file
Enter fullscreen mode Exit fullscreen mode

If this is the case for you it means you are not using Linux or macOS to run the command. You must likely be on a Windows Operating System or any other Operating System not supporting the command. In such cases use the first method, cat testkey.pub and copy whatever is the output or run type testkey.pub | clip.

Add the SSH key on GitHub

Navigate to your GitHub account's settings, select "SSH and GPG keys", and add a new SSH key. Paste the copied key into the designated field. Don't forget to fill in the "Title" field.

Configure the local Git command interface

To ensure ssh-agent is running run the command:

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

Update the SSH agent

Now add your SSH private key to the ssh-agent as show below:

ssh-add testkey
Enter fullscreen mode Exit fullscreen mode

The command above ensures that the local Git command interface recognizes the new key, run the following command.

Pushing Changes to a Remote Repository

To push your local repository to a remote repository (e.g., GitHub), follow these steps:

Identify the remote repository

Use the git remote -v command to view the remote repositories connected to your local repository.

Set the upstream repository

When pushing for the first time, set the upstream repository with the -u or --set-upstream flag:

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

This tells Git to push to the specified repository by default.

Subsequent pushes

For subsequent pushes, you can use the simpler command git push, as Git remembers the upstream repository.

Branches

Branches allow for parallel development and isolating specific features or fixes. Let's explore some common branch-related commands:

Creating a new branch

To create a new branch, use the git checkout -b command followed by the branch name. For example:

git checkout -b feature-readme-instructions
Enter fullscreen mode Exit fullscreen mode

The command above does two actions, it creates a new branch called feature-readme-instructions and then checks out to that branch.

Below is the command breakdown:

git branch feature-readme-instructions
Enter fullscreen mode Exit fullscreen mode
git checkout feature-readme-instructions
Enter fullscreen mode Exit fullscreen mode

Switching between branches

To switch to a different branch, use the git checkout command followed by the branch name. For instance:

git checkout master
Enter fullscreen mode Exit fullscreen mode

Viewing changes between branches

To see the code differences between the two branches, use the git diff command followed by the branch names:

git diff branchname
Enter fullscreen mode Exit fullscreen mode

The git diff command shows the differences between the current branch and the specified branch.

Merging branches

To merge changes from one branch into another, use the git merge command followed by the branch name. For example:

git merge branchname
Enter fullscreen mode Exit fullscreen mode

Deleting a branch

To delete a branch, use the git branch -d command followed by the branch name. For instance:

git branch -d branchname
Enter fullscreen mode Exit fullscreen mode

Resolving Merge Conflicts

Sometimes, when merging branches, conflicts may arise when Git cannot automatically merge the changes.

resolving conflict

Here's how to handle merge conflicts:

  1. Identify the conflicts: After attempting to merge branches, Git will notify you of any conflicts that occurred. You can use commands like git status or git diff to identify the conflicted files.

  2. Open the conflicted files: Open the conflicted files in a text editor or an integrated development environment (IDE). Inside the files, you'll see the conflicting sections marked with special characters like <<<<<<<, =======, and >>>>>>>.

resolving merge conflict

  1. Resolve the conflicts: Manually edit the conflicted sections in the files to keep the desired changes and remove the conflicting content. Modify the files according to your intended final result.
  • When manually editing the conflicted sections, you can make decisions about which changes to keep and which to discard. Git provides different options to handle conflicts: 1.) Accept Current Changes: This means accepting the changes that exist in your local repository before pulling from the remote repository. If you choose this option, you are discarding the incoming changes from the remote branch and keeping your local changes. 2.) Accept Incoming Changes: This means accepting the changes that exist in the remote repository. If you choose this option, you are discarding your local changes and taking the changes from the remote branch. 3.) Accept Both Changes: In some cases, you may want to combine both sets of changes from your local and remote repositories. You can manually edit the conflicted sections to incorporate both sets of changes, keeping the parts you want from each version.

accepting changes in GitHub

The specific options available to you may vary depending on the Git client or the merge tool you are using. It's important to review the conflicting sections carefully and make informed decisions about how to resolve the conflicts based on your desired final result.

4.) Add the resolved files: Use the git add command to stage the resolved files for the commit. For example, you can use git add file1.txt file2.txt to stage-specific files, or git add . to stage all modified files in the current directory.

5.) Commit the changes: Finally, use the git commit command to create a new commit with the resolved merge conflicts. You can provide a commit message using the -m flag, like git commit -m "Resolve merge conflicts".

Committing all modified files

Use the git commit -am "commit message" command to commit all modified files, including those added to the repository.

Note that git commit -am "commit message" is the same as combining the two commands below:

git add -A
Enter fullscreen mode Exit fullscreen mode
git commit -m "commit message"
Enter fullscreen mode Exit fullscreen mode

git add -A is the same as git add --all. Most developers prefer using period in place of --all or -A.

git add .
Enter fullscreen mode Exit fullscreen mode

Unstaging and removing specific files

To unstage and remove specific files, use the git reset <filename> command. If no filename is specified, git reset will show the files to be reset.

Undoing a commit

If you need to undo a commit, use the git reset HEAD~1 command to move the pointer (HEAD) to the previous commit.

Resetting to a specific commit

To reset to a specific commit, use the git reset <commit hash> command, replacing <commit hash> with the hash of the desired commit.

Removing all commits and untracked files

In some cases, you may want to completely remove all commits and untracked files. Use git reset --hard <commit hash> to accomplish this.

Collaboration with Forks and Pull Requests

When collaborating on open-source projects, forks and pull requests facilitate contribution. Here's an overview:

Forking a repository

To contribute to a repository, you can create a fork of the original repository. This creates a copy of the repository under your account.

Making changes and creating a pull request

After forking the repository, make your desired changes in the forked repository. Then, create a pull request to propose your changes to the original repository.

Additional Useful Commands

git clone

When cloning a repository in Git, there are a few different ways to specify the source repository. Here are the common methods to clone a repository using the git clone command:

Cloning via HTTPS

git clone https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

This method uses the HTTPS protocol to clone the repository. You need to replace user with the username and repo with the name of the repository you want to clone.

Cloning via SSH

git clone git@github.com:user/repo.git
Enter fullscreen mode Exit fullscreen mode

This method uses the SSH protocol to clone the repository. Again, replace user with the username and repo with the repository name.

Cloning via Git Protocol

git clone git://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

The Git protocol is a lightweight protocol specifically for Git. This method clones the repository using the Git protocol.

Cloning via Subversion (SVN) Bridge

git svn clone svn://svn.example.com/repo
Enter fullscreen mode Exit fullscreen mode

If the repository is a Subversion (SVN) repository, you can use the git svn clone command to clone it. Replace svn://svn.example.com/repo with the URL of the SVN repository.

Cloning a Local Repository

git clone /path/to/repository
Enter fullscreen mode Exit fullscreen mode

If the repository is already on your local machine, you can specify the local path to clone it.

Cloning a Bare Repository

git clone --bare https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

The --bare option is used to clone a repository as a bare repository, which means it will not have a working directory. This is useful when you only need the repository's history and don't plan on making changes directly in the cloned repository.

These are the main methods to clone a repository using the git clone command. Choose the appropriate method based on the type and location of the repository you want to clone.

Cloning Private Repository

When cloning a private repository on GitHub, you can use a personal access token (PAT) to authenticate and include it in the URL when using the git clone command. This allows you to securely clone the repository without having to enter your GitHub username and password.

The syntax for cloning a repository using a personal access token is as follows:

git clone https://<token>@github.com/<username>/repo.git
Enter fullscreen mode Exit fullscreen mode

Here's how to use it:

  1. Generate a Personal Access Token (PAT) on GitHub:

    • Go to your GitHub account settings.
    • Navigate to "Developer settings" and click on "Personal access tokens".
    • Click on "Generate new token" and provide a meaningful description for the token.
    • Select the desired scopes/permissions for the token. For cloning a private repository, make sure to include the repo scope.
    • Click on "Generate token" and make a note of the generated token.
  2. Clone the repository using the PAT:

    • In the git clone command, replace <token> with the personal access token you generated.
    • Replace <username> with your GitHub username.
    • Replace repo.git with the name of the repository you want to clone.

For example:

git clone https://your-token@github.com/your-username/repo.git
Enter fullscreen mode Exit fullscreen mode

By including your personal access token in the URL, Git will use it to authenticate and allow you to clone the private repository without any manual authentication steps.

Remember to keep your personal access token secure and avoid sharing it with others, as it provides access to your GitHub account.

If at any point you have generated a new token, you can run the command (in the repo directory) in the format shown below:

git remote set-url origin https://your-new-token@github.com/your-username/repo.git
Enter fullscreen mode Exit fullscreen mode

By running the command above, you will update the remote URL of the repository to use the new token, allowing you to authenticate and interact with the repository using the updated token.

git add

The git add command is used to stage changes for the next commit. You can use it to specify individual files or directories to add. For example:

git add file1.txt
git add directory/
Enter fullscreen mode Exit fullscreen mode

git log

The git log command displays a history of commits in reverse chronological order. It shows the commit hash, author, date, and commit message. You can use flags like --oneline for a more concise output or --graph for a graphical representation of branch history. For example:

git log --oneline
git log --graph
Enter fullscreen mode Exit fullscreen mode

git diff

The git diff command shows the differences between the working directory and the staging area (changes not yet staged) or between the staging area and the repository (changes staged but not yet committed). It helps you review changes before committing. For example:

git diff              # Show unstaged changes
git diff --staged     # Show staged changes
Enter fullscreen mode Exit fullscreen mode

git pull

The git pull command is used to fetch and integrate changes from a remote repository into the current branch. It combines the git fetch and git merge commands. For example:

git pull origin master
Enter fullscreen mode Exit fullscreen mode

This command fetches changes from the origin remote repository and merges them into the current branch.

Note: The code snippets provided assume a Unix-based operating system. Windows users may need to adjust certain commands or use alternative commands specific to their environment. For Windows users try installing a good terminal emulator like Git Bash.


References


Top comments (0)