Git Cheat Sheet

HOME

git config --global user.name "<Your-Full-Name>"
git config --global user.email "<your-email-address>"

git init
git clone <repository_url>
git clone --branch <branch_name> <repo_url>

git status
git add <file1> <file2> … <fileN>
git add .
git rm <filename_or_dir>
 git diff [file]
git commit -m “[Commit message]”
git commit --amend -m "New commit message"

git branch
git branch -a
git branch [branch-name]
 git rebase [branch_name]
git checkout  [branch_name]
git checkout -b  [branch_name]
git switch -c  [branch_name]
git branch -d [branch_name]
git branch -D [branch_name]
git branch -m [branch_name]
git merge [branch_name]

git fetch [remote]
git push origin branch
git pull
git pull --rebase
git push --all
git remote
git remote add [name] [url]
git remote rm [remote]
git remote rename [old_name] [new_name]

git stash
git stash list
git stash pop
git stash drop

git log
git log --all
git diff
git log --author="Name"
git log --until="2024-12-31"
git log [file]
git show

How to clone a Git Repository – git clone

HOME

git clone <repository_url>

git clone https://github.com/vibssingh/SpringBoot-Cucumber-TestNG.git

git clone -b <branch-name> <repository-url>

GIT Tutorials

HOME

Git is a Distributed Version Control System (VCS) which is originally developed in 2005 by Linus Torvalds (Creator of Linux) and is open source, i.e. freely available to use. It is the most popular and most used version control tool right now. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. 

Chapter 1 How to install Git on Windows 10
Chapter 2 How to create a new Git Repository – git init Command
Chapter 3 How to clone a Git Repository – git clone – NEW
Chapter 4 How to stage changes in Git – git add Command
Chapter 5 How to unstage the changes in Git – git rm command
Chapter 6 How to commit changes in GIT – git commit command
Chapter 7 How to track commits in Git – git log command
Chapter 8 How to commit an empty folder in GIT – gitkeep
Chapter 9 How to ignore files in GIT – gitignore
Chapter 10 How to create a branch in GIT
Chapter 11 How to stash changes in GIT – git stash command
Chapter 12 How to push new local GIT Repository to GitLab
Chapter 13 How to change a remote repository’s URL using git?
Chapter 14 Git Cheat Sheet – NEW

How to change a remote repository’s URL using git?

HOME

git remote -v

git remote set-url origin https://github.com/vibssingh/Serenity_Cucumber_JUnit5.git

How to stash changes in GIT – git stash command

HOME

The previous tutorial has explained about committing the changes in GIT. This tutorial explains about stashing the changes in GIT.

What is git stash?

git stash temporarily stashes or shelves the changes made in the working copy so that we can work on something else and later apply these changes.

Imagine a situation where you are working on updating a feature, but suddenly there is a production bug in the existing functionality of the feature that needs to be fixed immediately. So, we need to switch to a bug fix and leave working on the update work. As the update work is not completed, we can stash these changes and work on the bug fix. Later, we can re-apply our stashed changes back to the code.

The git stash command can stash the uncommitted changes (both staged and unstaged), save them away for later use, and then revert them from your working copy. 

How to reapply stashed changes?

To re-apply the stashed changes, use this command

git stash pop

This command removes the changes from your stash and reapplies them to your working copy.

Alternatively, you can reapply the changes to your working copy and keep them in your stash with : 

git stash apply

Stashing untracked or ignored files

If a new file is created and not staged, then git stash is not going to stash that file.

Adding the -u option (or –include-untracked) tells git stash to also stash your untracked files:

git stash -u

Congratulation!! We have learned about stash in GIT.

How to push new local GIT Repository to GitLab

HOME

This tutorial explains the steps that need to be followed to push a new local GIT Repository to GitLab.

What is GitLab?

GitLab is a single application that spans the entire software development lifecycle. GitLab is an open-source project maintained by GitLab Inc with over 3,000 contributors. We can install and self-manage the GitLab Community Edition which is fully open-source under an MIT license. GitLab also provides an Enterprise Edition that has additional features built upon the open-source edition. 

GitLab’s application offers functionality to collaboratively plan, build, secure, and deploy software as a complete DevOps Platform.

Create an empty repo in GitLab:

Step 1 – Login to GitLab using your username and password.

Step 2 – In the dashboard, click the blue New project button. This opens the New project page.

Step 3 – Select Create a blank project.

Step 4 – A new page will open. Provide the following information on that page:

1. Project Name – Mention the name of your project in the Project name field – GitTest.

2. Project slug – When a name is added, the Project slug auto-populates. This is the URL path for your project that the GitLab instance uses.

3. Project description (optional)  – This field enables you to enter a description for your project’s dashboard, which helps others understand what your project is about.

4. Visibility Level – Select the appropriate Visibility Level for your project. I have selected private option.

5. I’m not checking ReadMe option as I already have a ReadMe file in my project.

Select the Create Project button.

We can see that a new empty project is created in the GitLab as shown below.

Step 5 – For every remote repo, you will get a unique URL as highlighted below. This URL is used to push the local changes to this remote repo. Type the below command in GitBash:

git remote add origin https://gitlab.com/vibhasingh2004/gittest.git

To open GitBash, go to the location where the project is saved and Right Click and Select GitBash Here.

Add remote URL to local GIT Repository

git remote command provide the name to direct link to Repositories.

git remote command is used in conjunction with the git fetch, git push, and git pull commands.

git remote -v

This command list the remote connections we have to other repositories along with the URL of each connection.

Push the local changes to a remote repo

To push the changes from GIT Local Repository to GitLab in a particular branch of the remote repo, we need to use the below command:

git push <remote url or name> <branch name>

git push origin master

origin is the alias name of the full GIT Repository path (https://gitlab.com/vibhasingh2004/gittest.git)

Now run the Git push command as shown above. It will ask for your GitLab credentials in a new window as shown below. Please provide the username and password used to log in to GitLab.

Now we can see we have successfully pushed the local changes to the remote repository. Let’s go to GitLab and verify the latest changes. All the files from the local GIT Repository are moved to GitLab Remote Repository.

I hope this tutorial has helped you to set up a new project in GitLab and push the local changes to GIT Remote Repository.

How to pull changes from Remote Repository to Local Repository – git pull

HOME

The previous tutorial has explained about pushing the changes from Local GIT Repository to Remote GIT Repository. This tutorial explains the process of pulling the changes from Remote Repository to Local GIT Repository. I’ll use GitLab to make the changes.

What is git pull?

The git pull command fetches and downloads content from the remote repository and integrates changes into the local repository. The git pull command is called as the combination of git fetch followed by git merge.

This is my project in GitLab. If, you don’t have any project in GitLab, follow the below steps to create a GIT local Repository and push it to GitLab.

Step 1– To create a new Git local Repository, use git init. The details are mentioned in this tutorial – How to create a new Git Repository – git init Command.

Step 2 – To create a new project in GitLab, refer to this tutorial – How to create a new project in GitLab.

Step 3 – To push your GIT local repository to Gitlab, refer to this tutorial – How to push new local GIT Repository to GitLab.

Step 4 – Let’s add a new file in the Remote Repository directly using GitLab. Follow the steps shown below to add and commit a new .

Step 5 – This is a new file. I have provided name to this file as products.txt. Add the comment as commit message. Click the Commit changes button to commit this change.

Below image shows that new file – products.txt is committed to this project.

Step 6 – Go to the directory where local GIT Repository is saved. Right-click and click on Git Bash Here to open GitBash at that place.

Use the below command to see the list of files present in GIT Local Repository.

ls -a
git log

Type git log to see the list of commits present.

Step 7 – Type git pull command is used to pull the latest changes from Remote GIT Repository to local GIT Repository.

git pull origin master

This command says that pull the content from the “master” branch in the “origin” repo.

As the “git pull” command fetches and merges, we can see the newly added file named “products.txt” in my local repo.

Again use the below commands:

ls -a
git log

This shows that the new file – products.txt which was present in GitLab is now present in Git local Repository means latest change is pulled from GitLab to local Repository.

Congratulation!! We are able to pull the changes from Remote Repository to Local Repository. Happy Learning!!

How to create a branch in GIT

HOME

The previous tutorial has explained the committing of the changes in the GIT. This tutorial explains the creation of a new branch in GIT.

Branching is a feature available in most modern version control systems. Git branches are effectively a pointer to a snapshot of the changes. When we want to add a new feature or fix a bug, it is advisable to create a new branch to encapsulate the changes.  This helps you to clean up the future’s history before merging it. Git branches are an essential part of the everyday workflow. Git does not copy files from one directory to another, it stores the branch as a reference to a commit.

A branch represents an independent line of development. The git branch command allows us to create a new branch, and rename and delete the branch.

It’s important to understand that branches are just pointers to commits. When you create a branch, all Git needs to do is create a new pointer, it doesn’t change the repository in any other way. To create a branch, use this command:

git checkout -b branchName

Let me explain how to create a new branch with the help of an example:-

Step 1– Create a new GIT Repository – GitTest.

Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – GIT Repo has already committed files – Index.txt, ReadMe.txt, .gitignore. Create a GIT local branch feature/customerDetails.

git checkout -b feature/customerDetails

Now, the branch is changed from master to feature/customerDetail.

Step 4 – Update the already committed Index.txt file. Later, type git add . to stage the file to GIT Staging area. Then, commit the Index.txt file.

echo -en "\n A new page for Customer is added." >>Index.txt
cat Index.txt
git status
git add.
git commit -m "Updated Index.txt file with Customer Detail"

Step 5 – Below command shows that the updated Index.txt is committed in new branch feature/customerDetails

git status

Step 6 – Type the below command to go back to the master branch.

git checkout master

Step 7 – Open Index.txt file in the master branch. It shows that there is no change in this file, as the changes were done in another new branch. To get the latest changes in the master branch, we need to merge the new local branch (feature/customerDetails) to the master branch.

To delete a branch, use the below command:

git branch -d feature/customerDetails

As the branch hasn’t been merged, the above command output an error message as shown in the above image.

To delete the branch forcefully, even if the changes are unmerged. Below is the command:

git branch -D feature/customerDetails

To rename the current branch, use the below command:

git branch -m feature/newCustomer

To get the list of all the branches, use the below command:

git branch -a

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

How to ignore files in GIT – gitignore

HOME

The previous tutorial has explained the addition of an empty file or directory to the GIT using .gitkeep. This tutorial explains how to not stage certain files in GIT (ignored file).

What type of files are ignored?

Ignored files are usually built artifacts and machine-generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:

  • dependency caches, such as the contents of /node_modules or /packages
  • compiled code, such as .o.pyc, and .class files
  • build output directories, such as /bin/out, or /target
  • files generated at runtime, such as .log.lock, or .tmp
  • hidden system files, such as .DS_Store or Thumbs.db
  • personal IDE config files, such as .idea/workspace.xml

Imagine we have 100 files, and we want to commit only 99 files and ignore 1 file. This seems to be a very difficult situation.

To add the files to the GIT Staging area, we can use git add command. If we use git add ., it will add all 100 files to the GIT Staging area. Another way is to mention git add file1, file2….. soon, which is also a very tedious task.

Ignored files are tracked in a special file named as .gitignore named which is checked in at the root of the repository.  We need to mention the file we want to ignore in .gitignore file.

Let me explain how to use .gitignore with the help of an example:-

Step 1 – Create a GIT Repository named as GitTest.

Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – Type git status to check if any file is not committed.

Step 4 – Create a new directory – config.

mkdir config

Step 5 – Create a file named test.class into the directory.

touch config/test.class

Step 6 – Type git status to check that config folder is untracked.

Step 7 – Create a .gitignore file.

touch .gitignore

Step 8 – Add the folder or file we want to ignore.

echo config/ >> .gitignore

Step 9 – Type git status again. This time, we can see that the untracked file is .gitignore not the config directory.

Congratulations!!! We are done. Have a happy Learning!!

How to commit an empty folder in GIT – gitkeep

HOME

In the previous tutorial, I have explained how to commit changes in GIT. This tutorial will explain how to commit an empty folder or directory in GIT.

GIT can not push empty directory or folder. It can only track files.

If we try to push a folder with nothing in it, although it will exist on our local machine, but nothing will go into the branch. A common, standardized practice to solve this issue is to push a file called .gitkeep into the empty folders. This is not a feature of GIT.

Let me explain this with an example

Step 1 – Create a GIT Repository with name GitTest.

Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – Create an empty directory named as login.

mkdir login

Step 4 – Type git status to check if new directory is tracked by Git or not. This image shows that GIT didn’t track the new directory login.

Step 5 – Create a new empty file called .gitkeep into the login directory.

touch login/.gitkeep

Step 6 – Type git status again and now the new directory login is tracked by GIT.

I hope this tutorial has helped you to understand this concept. Have a nice learning!!!