Git cheat code

Understanding the concept behind Git

Anurag
12 min readJan 18, 2023

In this article, we will get to know some important things like what is Git & GitHub, why we need them, and how useful they can be for us.

Installing Git

To install Git, you will need to download the Git installation package for your operating system.

Git is available for macOS, Windows, and Linux. You can download the appropriate installation package from the following link:

Once you have downloaded the installation package, open it and follow the instructions to install Git on your computer.

After installing Git, you will need to configure it with your name and email address. This is important because Git records your name and email address with each commit you make. To set your name and email address, open a terminal window and type the following commands:

$ git config --global user.name "Your Name"
$ git config --global user.email "your_email@example.com"

Replace “Your Name” and “your_email@example.com” with your actual name and email address.

That’s it! You have now installed and configured Git on your computer.

Why do we need Git & GitHub?

Git is a version control system that helps you track changes to your code and collaborate with other developers. With Git, you can keep a record of your code changes, revert to previous versions, and collaborate with other developers on a project.

GitHub is a web-based platform that provides hosting for Git repositories. It is a central place where developers can share their code, collaborate on projects, and track the development process. GitHub offers a range of features that make it easy to review and collaborate on code, such as pull requests, code review, and project management tools.

Using Git and GitHub together allows developers to work on projects and track their progress efficiently, making it easier to collaborate with other developers and maintain a high-quality codebase.

init command

The git init command is used to create a new Git repository. It is typically used to initialize a new repository in an existing directory.

For example, if you have an existing project directory that you want to start version control with Git, you can navigate to the directory and type:

$ git init

This will create a new Git repository in the current directory.

The git init command creates a new .git subdirectory in your current working directory, which contains all the necessary Git metadata for the new repository. It does not make any changes to your code or files in the directory.

You can also use the git init command to initialize a new repository in a directory that does not yet exist. To do this, you can specify the path of the directory you want to create as an argument to the git init command:

$ git init /path/to/new/repository

This will create a new directory at the specified path, and initialize a new Git repository in it.

Untracked & Staged File

In Git, a file can be in one of two states: tracked or untracked.

An untracked file is a file that is not being tracked by Git. This means that Git is not keeping track of any changes made to the file, and the file will not be included in future commits.

A tracked file, on the other hand, is a file that is being tracked by Git. Git keeps track of any changes made to a tracked file, and the file will be included in future commits.

When you create a new file in a Git repository, it is initially an untracked file. In order to start tracking the file and including it in future commits, you will need to add it to the staging area.

To add a file to the staging area, you can use the git add command. For example, to add a file named file.txt to the staging area, you would type:

$ git add file.txt

This will add the file to the staging area, and it will now be a staged file. A staged file is a file that has been added to the staging area and is ready to be committed.

To commit the staged files, you can use the git commit command. This will create a new commit with the changes made to the staged files.

$ git commit -m "Add file.txt"

This will create a new commit that includes the changes made to file.txt. The -m flag allows you to specify a commit message, which should be a short description of the changes made in the commit.

git checkout command

git checkout is a Git command that allows you to switch between different versions of a repository. It can be used to switch between branches, tags, and commits.

Here are some common usage examples:

  • git checkout <branch_name>: Switch to the specified branch. If the branch doesn't exist locally, you can use the -b option to create it and switch to it in one command: git checkout -b <branch_name>.
  • git checkout <tag_name>: Switch to the commit associated with the specified tag. This is useful if you want to switch to a specific release or version of your code.
  • git checkout <commit_hash>: Switch to the commit specified by the commit hash. This is useful if you want to switch to a specific point in the commit history of your repository.

git checkout can be used to switch between different versions of a repository, but it is important to note that it can also be used to discard local changes. If you specify a branch or commit as the target of the git checkout command, and you have uncommitted changes in your working directory, Git will attempt to discard those changes and switch to the target version. This can be useful if you want to discard local changes and start over, but it can also be dangerous if you have important local changes that you want to keep. In that case, you should use a different command, such as git stash, to save your changes before switching to a different version.

git log command

git log is a command used to display a list of commits in a Git repository. It can be used to see the commit history of a repository, including the author of each commit, the date the commit was made, and the commit message.

Here is the basic syntax for git log:

git log [options] [<rev-list>]

The rev-list parameter is optional and can be used to specify a range of commits to display. If not specified, git log will display the commit history for the entire repository.

Here are some common options that you can use with git log:

  • --author=<pattern>: Only show commits where the author's name matches the specified pattern.
  • --grep=<pattern>: Only show commits with a commit message that matches the specified pattern.
  • --oneline: Display the commit history in a single line per commit.
  • --since=<date>: Only show commits made after the specified date.
  • --until=<date>: Only show commits made before the specified date.

For example, to display the commit history for the past week, you could use the following command:

git log --since='1 week ago'

You can use multiple options and specify a range of commits to customize the output of git log to suit your needs.

git diff command

git diff is a command that allows you to see the differences between two versions of a codebase. It can be used to compare changes you have made locally to a repository, changes between two branches, or changes between two commits.

By default, git diff shows you the changes that have been made to the code since the last commit. If you want to see the changes that have been made to a specific file, you can use git diff <filename>.

You can also use git diff to compare two branches by using the --branch flag, like this: git diff --branch <branch1> <branch2>. This will show you the changes that have been made to the code in one branch that are not present in the other.

There are many other options and flags that you can use with git diff to customize its behavior. For example, you can use --staged to see the changes that have been staged for the next commit, or --color-words to highlight the specific words that have been added or removed.

Removing file (rm command)

To remove a file from a Git repository, you can use the git rm command. This command will delete the file from your local file system and stage the deletion for commit.

For example, if you want to delete a file named example.txt, you would run the following command:

git rm example.txt

If you want to delete the file and commit the deletion in a single step, you can use the -f flag to force the commit, like this:

git rm -f example.txt

If you want to delete the file from the repository but keep a copy of it on your local file system, you can use the --cached flag. This will remove the file from the repository but leave it in your working directory.

git rm --cached example.txt

It’s important to note that once a file has been deleted with git rm, it cannot be recovered. If you want to keep a copy of the file, you should make a backup before running the git rm command.

git ignore command

git ignore is a command that can be used to tell Git to ignore specified files or directories. This is useful when you have files that you don't want Git to track, such as compiled binary files or temporary files that are created by your text editor.

To ignore a file or directory, you can create a file named .gitignore in the root of your repository and add the names of the files or directories that you want to ignore. For example, to ignore all files with the .log extension, you would add the following line to your .gitignore file:

*.log

You can also use wildcards and regular expressions to specify patterns of files to ignore. For example, to ignore all files in a tmp directory, you could add the following line to your .gitignore file

tmp/*

Once you have added the ignore patterns to your .gitignore file, you should commit the file to your repository so that the ignore rules are applied.

It’s important to note that git ignore is only used to ignore files that have not yet been added to the repository. If a file has already been added and committed, it will continue to be tracked by Git even if it matches a pattern in your .gitignore file. In this case, you can use the git rm command to remove the file from the repository.

Branches

In Git, a branch is a separate line of development. By default, Git repositories have a single branch called master, which is considered the main branch.

You can create additional branches in your repository to work on new features or bug fixes without affecting the master branch. This allows you to experiment with different ideas and isolate your changes from the main codebase until you are ready to merge them back into the master branch.

To create a new branch, you can use the git branch command followed by the name of the branch. For example, to create a new branch named new-feature, you would run the following command:

git branch new-feature

This will create a new branch, but you will still be on the master branch. To switch to the new branch, you can use the git checkout command followed by the name of the branch. For example:

git checkout new-feature

You can then make changes to the codebase on the new-feature branch and commit them as you would on any other branch. When you are ready to merge your changes back into the master branch, you can use the git merge command to combine the two branches.

It’s important to note that branches are just pointers to specific commits in the repository. When you create a new branch, it points to the same commit as the branch you are currently on. As you make new commits, the branch pointer moves forward to point to the new commit. This means that you can switch between branches at any time and your codebase will reflect the state of the repository at the time you switched branches.

git pull command

git pull is a command that is used to retrieve and integrate changes from a remote repository into a local repository. It is the counterpart to git push, which pushes local commits to a remote repository.

To use git pull, you need to specify the remote repository and the branch that you want to pull from. For example, if you want to pull the master branch from the origin repository, you would run the following command:

git pull origin master

This will retrieve the latest commits from the origin repository's master branch and merge them into your local master branch. If there are any conflicts between the changes, Git will prompt you to resolve them before the merge can be completed.

You can also use git pull to update a specific branch by specifying the name of the branch after the repository name. For example, to update the new-feature branch from the origin repository, you would run the following command:

git pull origin new-feature

By default, git pull will use the merge strategy to integrate the changes from the remote repository. However, you can use the --rebase flag to use the rebase strategy instead. This can be useful if you want to keep a linear history of your commits, as it will apply your local commits on top of the remote changes instead of creating a new merge commit.

It’s important to note that git pull is a combination of two other Git commands: git fetch, which retrieves the latest changes from the remote repository, and git merge, which merges the changes into the local repository. You can use these commands separately if you want more control over the process of integrating changes from the remote repository.

git push command

git push is a command that is used to transfer commits from your local repository to a remote repository. It is typically used to push commits that you have made on a branch to a shared repository, such as a repository hosted on GitHub or GitLab.

To use git push, you need to specify the remote repository and the branch that you want to push to. For example, if you want to push your commits to the master branch of a remote repository named origin, you would run the following command:

git push origin master

This will transfer all of the commits that you have made on your local master branch that are not present on the origin repository's master branch.

You can also use git push to create a new branch on the remote repository and push your commits to it. To do this, you can use the -u flag, which will set the upstream tracking branch for the new branch. For example, to create a new branch named new-feature on the origin repository and push your commits to it, you would run the following command:

git push -u origin new-feature

It’s important to note that git push will only transfer commits that are not already present on the remote repository. If you try to push a branch that is out of date with the remote repository, Git will prevent you from pushing your changes and suggest that you pull the latest changes from the remote repository before pushing.

git clone

git clone is a command that is used to create a copy of an existing Git repository. It is typically used to clone a remote repository, such as a repository hosted on GitHub, to your local machine.

To use git clone, you need to specify the URL of the repository that you want to clone. For example, to clone a repository from GitHub, you would run the following command:

git clone https://github.com/user/repo.git

This will create a new directory named repo in your current location, and it will copy the contents of the repository into this directory. The new directory will also be initialized as a Git repository, so you can start making changes and committing them right away.

You can also specify the name of the directory that you want to clone the repository into by adding it as an argument after the repository URL. For example:

git clone https://github.com/user/repo.git my-repo

This will create a new directory named my-repo and clone the repository into it.

It’s important to note that git clone only copies the content of the repository, not the commit history. When you clone a repository, the entire history of the repository is not transferred to your local machine. Instead, the history is retained on the remote repository and can be accessed through the origin remote, which is automatically created when you clone the repository.

--

--

Anurag

Currently working as Product Manager who is also a passionate engineer with an experience in Artificial Intelligence.