Git’s CheatSheet — All You Need

Charles Lo
13 min readFeb 25, 2023

--

Image source — https://phoenixnap.com/kb/wp-content/uploads/2021/07/git-distributed-architecture.png

Git is a popular version control system that allows developers to track changes in their codebase and collaborate with other team members. Git has become an essential tool in the software development industry, and mastering its commands can help you improve your productivity and efficiency. In this article, we’ll cover the most important Git commands and show you how to use them to manage your codebase.

Due to the length of this article, I have created a table of content for you to jump into the section you’re looking for.

Table of Content (Click to Jump)

Creating a Repository

Creating a new repository on Git is a simple process. Once you’re logged in to your account, click on the “+” icon in the top right corner of the screen, and then select “New Repository” from the dropdown menu.

From there, you’ll need to give your repository a name and description, choose whether it will be public or private, and select any other relevant options, such as whether to include a README file or add a license.

Once you’ve filled out all the necessary information, click “Create Repository,” and your new repository will be ready to use.

Example

Let’s say you want to create a new repository called “my-project” to store your latest coding project. Here’s an example of how you could do that using the Git command line:

1. Open up a terminal or command prompt on your computer.

2. Navigate to the folder where you want to create your new repository using the “cd” command. For example, if you want to create your repository in a folder called “Projects” on your desktop, you would type:

cd ~/Desktop/Projects

3. Once you’re in the correct folder, use the “git init” command to create a new, empty repository:

git init my-project

4. This will create a new folder called “my-project” in your current directory. Navigate into this folder using the “cd” command:

cd my-project

5. Now you’re ready to start adding files and making commits to your new repository. You can do this using commands such as “git add” and “git commit,” which we’ll cover in more detail in the following sections.

Output:

When you create a new repository on Git, you’ll typically see some output confirming that the repository has been successfully created. This might include information such as the repository’s URL, instructions for cloning the repository, and other details about the repository’s settings.

For example, if you created a new repository called “my-project” on Git’s website, you might see output similar to the following:

Initialized empty Git repository in /Users/your-username/my-project/.git/
remote: Create a README.md
remote: Create a .gitignore
remote: Create a license
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

This output confirms that a new Git repository has been created in the “my-project” folder on your local machine and that the repository has been initialized with a README file, a .gitignore file, and a license.

Cloning a Repository

Cloning a repository is the process of creating a local copy of a remote repository. To clone a repository, you need to have the URL of the remote repository. The syntax for cloning a repository using Git is as follows:

git clone <URL>

Here, <URL> refers to the URL of the remote repository. Once you execute this command, Git will download the entire repository to your local machine, creating a new directory with the same name as the repository. The repository will contain all the files, folders, and commit history from the remote repository.

Let’s say you want to clone a repository named “my-repo” from the URL “https://github.com/my-username/my-repo.git". To clone this repository, you would use the following command:

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

This will create a new directory named “my-repo” in your current working directory, which will contain all the files and commit history from the remote repository.

When you clone a repository, Git automatically sets up a remote named “origin” that points to the cloned repository. This allows you to fetch updates from the remote repository and push changes to it. You can view the remote repositories for your local repository by running the following command:

git remote -v

This will show you the URLs of the remote repositories that are currently connected to your local repository.

Output:

origin  https://github.com/my-username/my-repo.git (fetch)
origin https://github.com/my-username/my-repo.git (push)

In this example, the output shows that there is one remote repository named “origin” that points to the URL “https://github.com/my-username/my-repo.git". This means that you can fetch updates from this remote repository by running the command git fetch origin, and push changes to it by running the command git push origin.

Checking the Status of the Repository

Checking the status of a repository is an important part of working with Git, as it allows you to see the current state of your files and track any changes that have been made. To check the status of a repository, you can use the git status command. This will show you which files have been modified, which files are staged and ready to be committed, and which files are untracked.

For example, let’s say we have a repository with a few files in it, and we have made changes to one of the files:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: index.html

no changes added to commit (use "git add" and/or "git commit -a")

This output tells us that the index.html file has been modified, but has not yet been staged for commit. We can use the git add command to stage the changes and the git commit command to commit them to the repository.

Staging Changes

Staging changes is an important step in the Git workflow as it prepares the changes to be committed to the repository. Here is an example of how to stage changes:

1. Make some changes to a file in your local repository.

2. Run the following command to stage the changes:

git add <filename>

For example, if you made changes to a file called index.html, you would run:

git add index.html

3. Run the following command to check the status of the changes:

git status

The output will show the changes that have been staged for commit.

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: index.html

Note that the changes are listed under the “Changes to be committed” section.

4. Run the following command to commit the changes:

git commit -m "Added some new content to index.html"

This will commit the changes to the repository with a message describing the changes made.

[main 3e77903] Added some new content to index.html
1 file changed, 1 insertion(+)

The changes are now committed and can be pushed to a remote repository if desired.

Committing Changes

To commit changes in Git, you need to first stage the changes using the git add command, and then commit them using the git commit command. The git commit command creates a snapshot of the changes you have made and adds it to the history of the project.

To commit changes, you can use the following command:

git commit -m "Commit message"

Here, the -m option is used to specify the commit message. The commit message should be a brief description of the changes you have made.

For example, let’s say you have made changes to a file named index.html and you want to commit those changes. You can do it using the following commands:

git add index.html
git commit -m "Updated index.html"

This will stage the changes you have made to the index.html file and commit them with the commit message "Updated index.html".

The output of the git commit command will look something like this:

[master 1234567] Updated index.html
1 file changed, 2 insertions(+), 2 deletions(-)

Here, master is the name of the branch you are on, 1234567 is the commit hash, and Updated index.html is the commit message. The second line shows the changes made in the commit. In this case, there were 2 insertions and 2 deletions in the index.html file.

Viewing the Commit History

To view the commit history in Git, we use the git log command. This command lists all the commits that have been made to the repository, starting from the most recent one. Each commit is displayed with a unique commit hash, the name and email of the author, the date the commit was made, and the commit message.

Here is an example of how to view the commit history in Git:

git log
commit 30ea1e89f3d4ca4fa47d4b4f2706bae4136f0089 (HEAD -> main)
Author: Charles Lo <contact@charleslo.com>
Date: Fri Feb 18 15:21:56 2022 -0800

Updated README file

commit 5d8a74e50198795f2e2402749a1a9c9bfe0b5287
Author: Charles Lo <contact@charleslo.com>
Date: Fri Feb 18 14:45:12 2022 -0800

Added new feature X

commit 8e308d1cc22e67d1843e66e42470ab82d12f0dc3
Author: Charles lo <contact@charleslo.com>
Date: Thu Feb 17 19:33:51 2022 -0800

Fixed bug in feature Y

In this example, we can see that there have been three commits made to the repository. The most recent commit is at the top, and the oldest commit is at the bottom. Each commit is displayed with a unique commit hash (e.g. 30ea1e89f3d4ca4fa47d4b4f2706bae4136f0089), the name and email of the author (e.g. Charles Lo <contact@charleslo.com>), the date the commit was made (e.g. Fri Feb 18 15:21:56 2022 -0800), and the commit message (e.g. Updated README file).

The commit hash is a unique identifier for each commit and is used to refer to a specific commit in Git. We can use the commit hash to view the details of a specific commit or to revert back to a previous version of the code.

Branching

To view the current branches in your repository, use the git branch command. This will list all the local branches in your repository and show you which branch you are currently on, with an asterisk next to it:

$ git branch
branch1
* master
branch2

In this example, there are three local branches in the repository: master, branch1, and branch2. The current branch is master.

To create a new branch, use the git branch command followed by the name of the new branch:git branch <branch-name>

 git branch new-feature

In this example, we created a new branch called new-feature. Note that this only creates the branch, but does not switch to it.

To delete a branch, use the git branch command followed by the -d option and the name of the branch to be deleted:

git branch -d new-feature

In this example, we deleted the new-feature branch.

Viewing branching is an important part of working with Git. By understanding how to view and manage branches, you can effectively organize your code and collaborate with others on the same project.

Switching Branches

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

git checkout branch1
Switched to branch 'branch1'

In this example, we switched to the branch1 branch. You can now make changes to the code and commit them on this branch without affecting the master branch or any other branch.

Merging Branches

merging branches is an important part of Git that allows you to combine the changes from one branch into another. This is particularly useful when you have multiple developers working on different features or bug fixes at the same time.

To merge a branch into another branch, you can use the git merge command followed by the name of the branch you want to merge into your current branch. Here is an example:

# switch to the branch you want to merge into
git checkout main

# merge the feature branch into the main branch
git merge feature-branch

In this example, we are switching to the main branch and then merging the changes from the feature-branch into the main branch. Git will automatically try to merge the changes together, but if there are any conflicts between the two branches, you will need to resolve them manually.

Once you have resolved any conflicts and made any necessary changes, you can commit the merge using the git commit command. Git will automatically create a new commit that represents the merged changes.

Here is an example output of merging a branch:

# switch to the branch you want to merge into
git checkout main

# merge the feature branch into the main branch
git merge feature-branch

# resolve any conflicts and make necessary changes

# commit the merge
git commit -m "Merged feature-branch into main"

In this example output, we first switched to the main branch and then merged the changes from the feature-branch. We then resolved any conflicts and made any necessary changes before committing the merge with a message indicating that we merged the feature-branch into main.

Pushing Changes

Pushing changes involves uploading your local commits to the remote repository. To push changes, you need write access to the repository. You also need to ensure that your local repository is up to date with the remote repository to avoid merge conflicts. Here’s an example of how to push changes to the remote repository:

1. First, ensure that you’re in the correct branch by running the command:

git branch

This will show you a list of all the branches in your local repository, with an asterisk next to the currently checked-out branch. If you need to switch to a different branch, use the command:

git checkout branch-name

2. Next, make sure your local repository is up to date with the remote repository by running the command:

git pull

This command will fetch any changes from the remote repository and merge them into your local repository.

3. Once your local repository is up to date, you can push your changes to the remote repository by running the command:

git push

This will upload all of your local commits to the remote repository. If you have multiple branches, you can push a specific branch by running:

git push origin branch-name

Where origin is the name of the remote repository, and branch-name is the name of the branch you want to push.

Here’s an example output for pushing changes to the remote repository:

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:user/repo.git
2ac9b9d..5d693d5 main -> main

In this example, Git is pushing the changes in the main branch to the user/repo remote repository. The output shows the progress of the push, including the number of objects being pushed and compressed, and the total size of the data being written. The final line shows the commit hash of the last commit in the local branch, followed by the name of the remote branch that it's being pushed to.

Pulling Changes

When working on a project with multiple collaborators, it’s common to need to pull changes that others have made to the repository. Pulling changes allows you to update your local copy of the repository with the latest changes from the remote repository.

To pull changes in Git, you can use the git pull command. Here's an example:

git pull

This command will pull any changes from the remote repository and merge them with your local branch.

If there are any conflicts between the changes in the remote and local repositories, Git will prompt you to resolve them before the merge can be completed. You can use a text editor or a merge tool to resolve conflicts.

Here’s an example of what the output might look like after pulling changes:

$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 2), reused 5 (delta 2)
Unpacking objects: 100% (5/5), done.
From https://github.com/example/repository
3e4d4cb..83710a4 master -> origin/master
Updating 3e4d4cb..83710a4
Fast-forward
somefile.txt | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

In this example, Git has pulled changes from the remote origin repository and merged them into the local master branch. The output shows that Git has updated the somefile.txt file by adding two lines of code and removing one.

Configuring Git

Configuring Git is an important step to ensure that your commits are properly recorded and attributed to you. Here is an example of how to configure Git:

1. Set your name and email:

git config --global user.name "Charles Lo"
git config --global user.email contact@charleslo.com

This will set your name and email to be associated with your Git commits.

2. Set your default branch:

git config --global init.defaultBranch main

This sets your default branch to be “main” instead of the default “master” branch.

3. Set your text editor:

git config --global core.editor nano

This sets your default text editor to be Nano. You can replace “nano” with your preferred text editor.

4. Set your Git credentials:

git config --global credential.helper store

This stores your Git credentials so that you do not have to enter them every time you push or pull from a repository.

5. Check your Git configuration:

git config --list

This will display your current Git configuration.

Output:

user.name=Charles Lo
user.email=contact@charleslo.com
init.defaultBranch=main
core.editor=nano
credential.helper=store

By configuring Git, you can streamline your workflow and ensure that your commits are properly attributed to you.

Conclusion

Git is a powerful tool that can help you manage your codebase and collaborate with other developers. By mastering these essential Git commands, you can become a more efficient and productive developer. With this cheat sheet, you should have all the information you need to get started with Git and take your development skills to the next level.

More Stories you maybe interested?

About the Author

Hi Medium Community, my name is Charles Lo and I’m currently a project manager and data manager at Luxoft. Luxoft is a place where we combine a unique blend of engineering excellence and deep industry expertise to serve clients globally, specializing in cross-industry including but not limited to automotive, financial services, travel and hospitality, healthcare, life sciences, media, and telecommunications. In addition, Luxoft is also a family member of DXC.

I’m passionate about technology and hold several certifications including Offensive Security Certified Professional, AWS Certified Solution Architect, Red Hat Certified Engineer, and PMP Project Management. I have years of experience working in the banking, automotive, and open-source industries and have gained a wealth of knowledge throughout my career.

As I continue on my Medium journey, I hope to share my experiences and help others grow in their respective fields. Whether it’s providing tips for project management, insights into data analytics, or sharing my passion for open-source technology, I look forward to contributing to the Medium community and helping others succeed.

Author Linkedinhttps://www.linkedin.com/in/charlesarea/

--

--

Charles Lo
Charles Lo

Written by Charles Lo

⭐ Tech pro passionate about cloud computing, security, programming, open-source, and project management. Sharing expert insights to drive innovation and growth