- Introduction
- What is Git?
- Top 30 Git Cheat Sheets in 2024
- How to check Git Configuration?
- How to set up Git?
- Starting a project in Git
- How to cache your Git login credentials?
- How to initialize Git repo?
- How to add files to the staging area in Git?
- How can we check the repository status in Git?
- How to add only certain files in Git?
- How can we commit changes in Git?
- How to commit changes with a message in Git?
- How can we see commit history in Git?
- How can we see a specific commit in Git?
- How to see log stats in Git?
- How can we see the changes made using git add -p?
- How can we remove the tracked files from the current working tree?
- How can we rename files in Git?
- How to ignore files in Git?
- How to revert staged and unstaged changes in Git?
- How can we amend the more recent change in Git?
- How can we rollback the last commit in Git?
- How can we create a new branch in Git?
- How can we list branches in Git?
- How can we create a branch and switch to it immediately in Git?
- How can we delete a branch in Git?
- How can we merge two branches in Git?
- How can we see remote URLs in Git?
- How can we push a new branch to a remote repo in Git?
- How to access the commit history?
- How to remove files in Git?
- Conclusion
Introduction
Git has gained popularity as the leading Version Control System in today’s digital world. Developed by Linus Torvalds in 2005 who was also the creator of Linux, Git is responsible for many things that happen locally on your system. While Git is flexible and easy to use, it can be hard to remember all its commands. This article ” Git Cheat Sheet” will serve as a quick reference guide to Git commands and basics for beginners as well as advanced developers.
What is Git?
Git is an open source distributed system that is designed to support developers to collaborate on projects of small as well as large scale. It was originally developed to control and manage the Linux kernel’s development. As a distributed version control system, Git is responsible for keeping track of changes in the system files and coordinating those working on the project. This source code management system is designed for speed and accuracy. It is a simple to use a repository that facilitates the management of files and projects over a period of time. Therefore, it makes the collaboration process easier and more seamless. Every team member has the access to the backup on their local machine, thanks to external servers such as GitHub. This information can also be copied and the entire team can view the changes made to them.
Top 30 Git Cheat Sheets in 2024
How to check Git Configuration?
Git has numerous commands that return with specific information that you requested. Here is an example of a command that checks configuration –
git config -l
To check the user name and email, use this command –
$git config — global user.name”firstname lastname”
$git config — global user.email” abcxyz@erm.com”
How to set up Git?
To set up your Git username, use the following command –
git config –global user.name “Marie”
To set up Git user email, use this command –
git config –global user.email “signups@fabiopacifici.com”
Starting a project in Git
To create a new project in Git, first create a repository. Now, add the page to that repository. For example –
git add hello.html
git commit -m “First Commit”
How to cache your Git login credentials?
It is easy to store the login credentials in the cache so that you do not have to type them out manually each time. Using this command, you can cache your credentials –
git config –global credential.helper cache
How to initialize Git repo?
You can initialize Git repo in your project root using this simple command –
git init
How to add files to the staging area in Git?
You can easily add the desired file in the staging area by simply replacing the ‘file name’ with the name of your file using this command –
git add filename_here
Now, if you wish to add all files to the staging area, use the ‘.’ and every file in your project will be added. Here is the command for it –
git add.
How can we check the repository status in Git?
You can check the status of the current repository using this command –
git status
How to add only certain files in Git?
Additionally, for certain files, you can use the ‘*’. To add all files in the staging area that start with the book, use this command –
git add fil*
How can we commit changes in Git?
Using this command, you can open up a text editor that can help you do modifications anytime –
git commit
How to commit changes with a message in Git?
There is a way to commit changes in Git without opening up the text editor. Using this command, you can specify a short summary for your commit message –
git commit -m “your commit message here”
How can we see commit history in Git?
You can check the commit history for your current repository using this command –
git log -p
How can we see a specific commit in Git?
A specific commit can be seen by using this command and inserting your commit id –
git show commit-id
How to see log stats in Git?
The log stats such as recent changes made in the commit, file names, etc. Can be checked using this command –
git log –stat
How can we see the changes made using git add -p?
If you want to see the changes, the git adds command lets you add content into the staging area. Git adds -p gives you a lot of options including the option to stage changes or not.
How can we remove the tracked files from the current working tree?
The easiest way to do this is by using ‘git clean’ command. Also, you can know why a file was deleted using the command – ‘git rm filename’. This command gives a commit message explaining why it was deleted in the first place.
How can we rename files in Git?
The ‘git mv’ command helps in renaming files in git as well as in moving files to other location. You can use this command to stage the change and rename a particular file –
git mv oldfile newfile
How to ignore files in Git?
The ‘git ignore’ command is used to ignore files. It excludes certain files from your directory and you can share the results with anyone. You can use this command in the prompt to do so –
.gitignore file
How to revert staged and unstaged changes in Git?
For unstaged changes:
The ‘git checkout’ command lets you discard or revert local changes in a file permanently. You can undo changes in Git by using this command –
git checkout filename
However, to revert all local changes to all the files permanently, you can use this command –
git reset –hard
For staged changes:
For specified changes in the files, you can use the ‘-p’ option and the following command –
git reset HEAD filename
git reset HEAD -p
How can we amend the more recent change in Git?
The ‘git commit–amend’ command is the convenient way to change your most recent commit. This command does not simply change the commit but replaces it totally. The command can also be used to edit the earlier commit messages. Also, instead of creating a new commit, you can use this command to combine staged changes with the previous commit. Here’s the command –
git commit –amend
How can we rollback the last commit in Git?
The ‘git revert’ command will help you undo the last commit in Git. This command will create a new commit along with changes introduced by the reversal of the last commit in git. You can use this command and insert the head alias –
git revert HEAD
How can we create a new branch in Git?
In git, there is always a default or main branch. If you want to create an entirely new branch, you can do so by creating it from the main branch. The ‘git checkout’ command helps in the creation of a new branch which is different from the main one. However, you have to manually switch to it using another command.
$ git checkout -b new-branch dev-branch
Switched to branch ‘new-branch’
Alternatively, you can use the ‘branch’ command as well to create a new branch. Here is the command –
git branch branch_name
How can we list branches in Git?
In the Git, you can branch out from the main code base to collaborate easily with other developers. To see the current branch, you can run this command –
git status
Now, once you know what branch are you on, you can use this command to see all local branches –
git branch
This command will return the information on all the branches and the local branches will be highlighted in green and marked with an asterisk.
If you want to see all the remote branches, use this command –
git branch -r
To see both remote and local branches, you can run this command –
git branch -a
How can we create a branch and switch to it immediately in Git?
You can create a new branch using this easy command –
git checkout -b my-branch-name
As you are ready to now commit to a new branch, you can switch to it right away. If you want to switch to your branch in the local repository, use this command –
git checkout my-branch-name
If you want to switch to a branch that came from a remote repository, run this command –
git checkout –track origin/my-branch-name
How can we delete a branch in Git?
If you have completed working on a new branch and merged it as well, you can delete it using this command –
git branch -d branch_name
The above mentioned command will delete a branch from the local repository. Here. ‘-d’ option deletes only that branch that has been merged.
This command deletes a branch irrespective of its merged status –
git branch -D my-branch-name
However, if you want to delete a remote branch, use this command –
git push origin –delete my-branch-name
How can we merge two branches in Git?
First, you can start by knowing the status of what branch are you on using this command –
git status
Now, you can merge the history of the current branch with a specified branch name using this command if you are already on the branch –
git merge branch_name
To checkout the branch you want to merge into another one, run this command and replace ‘master’ with the specified branch name as needed –
git checkout master
How can we see remote URLs in Git?
First, you have to add the remote repository to your local one to be able to add remote URLs later. You can run this command to do so –
git add remote https://repo_here
Once you have all the information, you can see all the remote URLs by running this command –
git remote -v
How can we push a new branch to a remote repo in Git?
You can save your work on a remote repository once it is finished and push a new branch using this simple command –
git push
How to access the commit history?
There is a log built in Git as commit after commit gets logged. You can use this command to access the commit history on a remote repository –
git log origin/main
How to remove files in Git?
To remove files, you can simply run this command and add the desired file name that you wish to remove –
$git rm -r <filename>
Conclusion
Compared to other software or apps, Git is faster and more flexible. It makes the collaboration and workflow easy even when the team is working from remote locations. Additionally, it is convenient to merge the code into another system using Git. These commands will help you in improving your team’s overall productivity while using Git. You can bookmark this guide for future reference as well. This will provide excellent instant support for beginners and advanced developers. Check out the free git courses to further your learning.