In this guide, we have compiled all the important and most used Git commands together with their usage. Follow till last to get yourself comfortable with Git and version control. Trust me if you get acquainted with Git once you won’t be able to resist using it. It is such a beautiful thing.
Table of Contents
Often beginners confuse Git with GitHub and it is understandable due to the huge popularity of GitHub. GitHub is a cloud hosting service that lets you manage your Git repositories and collaborate with other people using Git. While Git is an open-source distributed version control system that helps track changes and versions of your work.
How Git Works?
Git is an open-source distributed version control system, which means you can too contribute to the code of Git and make it better. It works by distributing work to many levels and people and saves only changes each time with author and date. Anyone can pull a copy of your project and keep it, they can add changes to it as well and can request you to merge them into the original version of the project i.e. your copy.
We should learn Git commands along with other GUIs as it gives better command over the system because with commands we understand each and every action and what it is doing. Also using CLI is faster than using GUIs.
Installation of Git
In Windows 10
Git has both CLI and GUIs available but we will learn Git CLI in this guide, Git GUIs are self-explanatory, though you can follow the guide for GitHub Desktop here. You can download the Git CLI from its official site for Windows, macOS or Linux. On Windows the installation process is very simple, like any other software, you just need to press the “Next” button a few times and the Git CLI will be ready to work. You need not do anything on the installation screen as default settings are perfect for most people.
If you have installed XCode or its Command Line Tools on your Mac then Git may be already installed on your device. You can check the version of Git using the command
If you do not have Git installed, you need not worry at all. You can download the Git for Mac installer and follow the prompts to install Git on your Mac. Or, if you have installed Homebrew to manage packages on OS X then you can follow the command below to install Git on your Mac.
$ brew install git
You can verify the installation by checking the version of Git using the command given above.
The process of installing Git on Linux is very similar to that of macOS. You just need to type
sudo apt-get install git on the terminal and Git will be installed. You can verify the installation by checking the version of Git installed.
sudo apt-get update sudo apt-get install git git --version
We have prepared this tutorial on a Windows device, but the commands and their usage remains the same in all the environments and it works with the same efficiency. You can seamlessly follow the guide irrespective of your operating system and its version.
What is Git Bash?
Git Bash is basically an application that provides a Unix based shell to emulate the Git CLI on Windows environment. Bash is the acronym for Bourne Again Shell and a shell is a terminal application that lets users connect to the operating system through the command-line interface. Git Bash is a package that installs Bash, some Bash utilities and Git on Windows environment. It helps us connect to and control some Git elements and our project repositories using commands.
Git Bash as mentioned above is a Unix based shell and it lets users use Git on it that does not mean it is meant for Git only, you can use Git Bash as a full-fledged terminal that is quite similar to the terminal in Linux.
Basic Git Commands and their Uses
We will now explore how to use various Git commands and will understand the operations performed by Git. To understand this better we will create a separate folder somewhere and inside the same folder, we will right-click and run Git Bash inside which we will be running and testing our Git commands. Also, create 2-3 dummy HTML files for understanding the concepts more practically.
git config --global user.name your_nameconfigures the user globally for the system and every commit in that system is by default registered to that user.
git config --global user.email your_email_idsimilarly configures your email-id globally for commits.
git config --global user.namereturns the name of the global user, similarly, you can view the email id of the user using
git config --global user.email.
git config user.name your_nameand
git config user.emailcan be used to set repository-specific username/email configuration. For this, you need to move to the working directory in Git Bash or directly open the Git Bash inside the working directory.
code.opens the VSCode, I use VSCode, you may use any text editor according to your convenience.
git initinitializes a new Git repository from scratch and creates a hidden folder within the directory that tracks the work of the repository for version control. You can also clone an existing repository using the command
git clone repo_url.
ls -lartis a Linux command which shows all the hidden files in the current directory. You can see more Linux commands here along with the complete meaning of each letter used.
git statusshows the status of the files in the directory whether they are untracked, staged, unmodified, modified or committed. You can better understand the states of a file with the illustration given below.
git add your_file.extensionadds the file to the staging area and hence takes a snapshot of the changes performed in the file and includes it in the project’s history.
git add -Aadds all the untracked files to the staging area. Then if you change the files,
git statusreturns the modified tag beside the file. You can again add the modified files to the staging area using the
git add -Acommand.
git commitcommits the staged files finally. After
git commityou will be asked to enter a commit message, you should know how to edit files in Vim.
git commit -m "Your_commit_message"is the command which directly commits the files with a given commit message.
git checkout your_file.extensionmatches your files to the last commit and restores the earlier version. It is useful if something went wrong in a particular file and you want to restore the earlier version of it.
git checkout -fmatches the whole directory to the last commit and restores the earlier version of all the items in the directory.
git logshows who committed what and at what time along with the commit messages. You can exit out of the log by pressing “q” from the keyboard.
git log -p -5shows the same details of the last 5 commits. Similarly, if you want to view the details for the last two commits, you can use
git log -p -2.
git diffcompares working untracked files with the staged files and highlights the changes. If we add all the modified files to the staging area then it returns nothing as there is nothing to compare.
git diff --stagedcompares staging area with the last commit and highlights all the changes.
git commit -a -m "your_commit_message"skips staging and directly commits all the files. It is not recommended most of the times.
git rm your_file_name.extensiondeletes the file from the staging area as well as from the working directory.
git rm --cached your_file_name.extensionremoves the file only from the staging area.
git status -sis similar to
git statusbut it shows the status using symbols. An example of the output of
git status -sin different scenarios is given below.
#When all the modified files are in the working directory and still not staged. M file_1.html M file_2.html M file_3.html #When only 'file_1.html' is staged using 'git add file_1.html'. M file_1.html M file_2.html M file_3.html #Again if 'file_1.html' is modified then 'git status -s` returns. M M file_1.html M file_2.html M file_3.html
What is .gitignore?
Git is an advanced version control system and an advanced system, it cannot follow a space-consuming architecture. So being smart, it tracks only source codes and ignores the automatically generated files like logs, output files of the source code and other automatically generated files. At the same time, to prevent any mishap it gives full control of ignoring such files to the master. Hence, we can create a
.gitignore file and list all the unnecessary files which are not important and can slow down your push/pull speed.
touch .gitignoreis a bash command used to create
.gitignorefile in the working directory. In the Windows environment, it is difficult to create
.gitignorein file explorer so
touchcommand is used in bash to create the file.
You can now open
.gitignore in any advanced text-editor like VSCode and list down all the files which need not be tracked by Git.
- If you list
.gitignorethen it will ignore the mentioned file from the root directory as well as from all the subdirectories of the working directory.
*.extensionwill ensure that any file in all directories and subdirectories with extension
.extensiongets ignored. It could be any extension (I used
.extensionfor reference it could be any extension like
/your_file_name.extensiononly ignores the file in the root directory where
folder_name/ignores and complete subdirectory or folder.
Also Read: 10 VSCode Tips and Tricks You Should Know
Branches are basically separate copies of the main/master branch that are meant for development or modification purposes. For example, if a project is in production and you want to modify it or develop some new feature in it then, it is obvious that you won’t like to try changing the project on production directly, you will try and test your changes before pushing it into the production. The same feature is provided by Git with the help of branches.
git branch branch_namecreates a branch with the name
branch_namewhich is an exact replica of the master branch but any operation performed in this branch won’t be reflected in the master branch. You will have to manually switch to the new branch if you want to work in the new branch.
git branchgives a list of all the existing branches of the current project. It gives a star beside your current branch to specify which branch you are currently in.
git checkout branch_namechanges your current branch to
branch_namewithout affecting your work in the current branch.
git branch -m oldname newnamechanges the name of the branch
git branch -m newnamechanges the name of the current branch to
git merge branch_namemerges the work of the branch
masterbranch. Run this command being on
masterbranch. The command
git log -p -5will show the last 5 commits including the commits of the branch
git checkout -b branch_namecreates a branch named
branch_nameand switches to that branch automatically. You need not switch to the new branch manually if you use this command.
Remote Repository on GitHub
After creating your GitHub account, there are two ways of authenticating the account on Git, a sign-in pop up while you add a remote repository to the Git project or using an SSH key. Using an SSH key is more secure and satisfying and it is a must for private repositories. You can follow this guide to generate and link the SSH key to your account. You can also find the link to the guide on your GitHub account’s ‘Settings’ under the ‘SSH and GPG keys’ tab.
Now, you can create your first repository on GitHub by clicking on the ‘New’ button and fill in all the details for the new repository. You can add a Readme file too which will contain all information about your repository. You can use tools like readme.so to create an intuitive readme file very quickly.
You can get the URL and SSH to any public repository by clicking on the ‘Code’ button as shown in the image below.
git remote add origin your_github_repo_url/sshlinks your remote repository to the current project and gives the name
originto the particular remote repository.
git remoteshows all the names of the remote repositories linked with the current project.
git remote -vshows all the linked remote repositories along with their URLs.
git push origin masterpushes your local
masterbranch to the master branch of the remote repository added with the name
git push -u origin masterpushes the branch
masterto the remote repository and sets it to default such that any further pushes using
git push origin masteris a push from the local
git push origin other_branch_namepushes the branch
other_branch_nameto the remote repository.
git push origin --allpushes all the branches from the local repository to the remote repository named
git fetch origindownloads all history from remote-tracking branches.
- To change the URL of the remote repository named as
originyou can use
git remote set-url originnew_url/ssh.
git clone repo_urlclones an existing repository (whose URL is provided) to the current folder with the repository’s name.
git clone repo_url folder_nameclones an existing repository with the name
Change Default Repository in GitHub
There has been a recent update in GitHub, according to which the default repository in GitHub has been named as
main, earlier it was
master. So, if you push your local
master branch to the remote repository on GitHub, it will create a new branch
master. You can change the default branch of that repository by heading to ‘Repository Settings’ >> ‘Branches Tab’ >> ‘Switch to another branch’. See the image below for reference.
You can also change your default branch settings for all your repositories by going to ‘Account Settings’ >> ‘Repositories Tab’ >> ‘Update default branch name’. See the image below for reference.
Push to remote main branch from local master
You can also push directly to the main branch from your local master branch using the command:
git push origin master:main
This guide is a very good platform for beginner’s to get introduced to Git and start using it in their projects. The commands provided above are some of the most used Git commands and most of the time, beginners won’t be requiring other Git commands but if you need to perform some other action, you can always try putting your queries on Google and explore the solutions. We have tried to give you a start with Git, there are much more things you can do with Git and GitHub. The more you explore, the more you will learn.
We hope you liked this article, there are many more such guides on our blog. You can try reading the complete list of Linux commands for beginners and 100 Windows 10 shortcuts that can make your life easy.
Kindly share this article with your friends or colleagues who are looking towards starting off with Git and GitHub. You can comment down your queries in the comment section below, let us have a fruitful discussion.