Mastering Git: Your Ultimate Git Cheat Sheet for Quick Reference

Git is an open-source distributed version control system developed by Linus Torvalds in 2005. It is used for versioning, collaboration among developers and tracking purposes. In this post, I will list down the most common Git commands used by developers and QA. Check out this Git cheat sheet for quick and easy reference in your next interview.

Git Cheat Sheet

Git Cheat Sheet

Initializing and Cloning Git Repository

git init: This command initializes a new empty repository in the current directory. It also creates a hidden .git folder to store all the version control information.

How do I clone a Git repository?

git clone: This command is used to clone the remote git repository in your local machine.

git clone [reponame]

Git Branch Commands

git branch: Lists all branches in the repository. The current branch is indicated with an asterisk.

Create a Git Branch: To Create a new Git branch use below mentioned command.

git branch [branch name]

Switch to a Git branch: To switch to an existing branch use the checkout command.

git checkout [branch name]

git checkout -b [branch name]: This Git command creates a new branch and switches the Head pointer to the newly created branch.

git branch -r: This is used to list only the remote git branches.

git branch -a: This Git command is used to list all local and remote branches.

git help branch: This command shows all the available help options related to the branch command.

Rename a branch: To rename a Git branch use the [git branch -m “new name”] Command.

Git Delete Branch

How to Delete a Git Branch Locally?

An important point to understand here is that Git does not allow you to delete your current branch (on which you are currently working). To delete a branch first move to another branch or master branch and then delete the desired branch.

git checkout [master]
git branch -d [branchName].

The above-mentioned commands will delete the local branch.

How to Delete a Git Branch Remotely?

To delete a Git Remote branch you need to use the below-mentioned command.

git push origin --delete [branchName]

How to Delete a Branch Forcefully?

If you have unmerged changes in your branch but still want to delete that branch without merging the changes then use ‘-D’ to force delete the branch.

git branch -D branch name

Adding And Committing Files in Git

git add [file(s)]: This command stage changes for commit. Using the following command, you can specify which files to include in the next commit. By using this command the unstaged files are added to the staged area from the workspace.

git add [filename]

git add -A Or git add. : These commands staged all files including new files, Modified files and removed files.

git status: Shows the current status of the repository, including any changes that are staged or not yet tracked.

git commit -m “[commit message]”: Commits the staged changes to the repository. A commit message is required to describe the purpose of the commit.

How to stage and commit changes together

Either first use the git add command to add/stage all the changes and then use git commit to commit those changes to the local repository or use just one command mentioned below to perform both operations in one go.

git add -am “[Commit message]”

Git Commit History

git log: Shows the commit history.

git log -p: Displays the commit history along with the changes done. The diff output shown by the -p flag provides a detailed view of what exactly was modified in the files between each commit.

git log -p filename: This command allows us to view the commit history and changes for a specific file, rather than displaying the changes for all files in the repository.

How to filter Git logs?

We can filter git logs on the basis of time frame. See the below examples to understand more:

git log -3 

The above command will filter out the git logs by 3 recent commits.

git log —since=2022-08-08
git log --since="2023-01-01" --until="2023-08-31"

We can use the --since and --until options to filter logs by a specific date range.

We can use the --author option to filter logs by a specific author’s name or email.

git log --author="John Doe"

We can also use the --grep option to filter logs by committing messages containing a specific text. Refer to the below command:

git log --grep="bug fix"

We can use the -- <filename> option to filter logs to only show commits that affected a particular file.

git log -- <filename>

Other options are also available to filter out Git Logs.

git log –oneline:-This command shows the simplified version of the commit history, showing each commit as a single line with a concise commit message along with its corresponding commit hash in reverse chronological order.

abc1234 Fix bug in user authentication
def5678 Add new feature: user profile images
ghi9012 Update README with project description
jkl2345 Refactor database query code

Git Show Commands

git show HEAD: This command displays detailed information about the specific commit and can be used in multiple ways.

1) With Commitid: Displays the commit information for the provided commit ID.

git show commit-id

2) Show previous commits: Display the commit information of parents or grandparents.

git show Head^^ 
git show Head~2

3) Show Differences Between Two Commits: This command shows the difference between 2 commits

git diff <commit_hash1> <commit_hash2>

Git Cherrypick Command

The Git cherry-pick command is useful for cherry-picking the specific commit instead of picking all commits and applying them to another branch.

First checkout to the branch where you want to apply the cherry-pick changes. For example, I want to pick some changes from branch1 and apply them to the master branch then follow the below-mentioned steps:

git checkout master

Now cherry-pick the commit ID of those changes that we want to apply in our master branch.

git cherrypick committed

Git Stash Commands

git stash: Temporarily stores changes that you don’t want to commit immediately. This allows you to switch branches without committing to unfinished work.

git stash –all: This command stashed everything.

git rm filename: Removes the file from both the local and remote repository.

Push, Pull and Merge

git pull: Fetches changes from a remote repository and merges them into the current branch. It’s used to update your local branch with the latest changes from the remote.

git push: This command sends your committed changes to a remote repository, updating it with your local changes.

git fetch: Retrieves changes from a remote repository without automatically merging them. It updates your local repository’s knowledge of remote branches.

How do I merge branches?

git merge [branch name]: This command integrates changes from one branch into another. It combines the changes and creates a new commit to represent the merge. It’s best practice to switch to the branch in which we want to merge the changes. If I want to merge the changes in the master branch then first move to the master branch and then use the merge command.

Merge Git Branch

There are 2 types of merge a)fast-forward & b)True merge (Git automatically handles it)

How to handle merge conflicts in Git?

  1. Abort it using [git merge –abort]
  2. Manually correct it 

Revert and Reset

git reset [file(s)]: Unstaged changes from the staging area. It removes changes from the next commit but keeps them in your working directory.

We can undo as many commits as we want by changing the number after the tilde.

git reset HEAD~1

Git reset works in three different modes.

  1. git reset –soft: This option will reset your branch to the previous commit, but it will leave your changes staged (you can commit them again if needed).
git reset --soft HEAD~1

2. git reset –mixed: This option will reset your branch to the previous commit and unstage your changes. Your changes will still be in your working directory as unstaged changes. You need to index them and commit them again if needed.

git reset HEAD~1

3. git reset –hard moves the branch pointer, unstages your changes, and discards all changes in your working directory. It effectively removes the commits and all changes.

git revert is generally used when you want to undo a specific commit without altering the commit history. It’s useful in shared repositories because it preserves the commit history and allows us to track when and why changes were reverted.

git revert head~1 Or git revert id

The above command will create a new commit.

More :

git diff –color-words: This command allows you to see word-level differences between two branches or commits. To specify a conflict within a specific file, you can use the following syntax:

git diff --color-words master..branchname -- path/to/filename

git blame: This command helps to view the commit history for each line in a specific file. It’s useful for identifying who made changes to a particular line of code and when those changes were made.

Git blame filename 

How do I create a Git tag?

git tag tagName: Git tags are used to denote releases or milestones. Two types of tags are available lightweight tags and annotated tags.

To show tag
git show tag name or git tag -l “v1.*”

push tags to remote
git push origin tag name

Push all the tags at once
Git push --tags (local)
Git push origin --tags (Remote)

Delete tag from local
Git tag -d tag name
Git tag --delete tag name

To remove tag from origin
Git push origin -d tag name

To learn more about Git and Git commands you can visit this document.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading