A Quick Glance The Most Useful Git Commands
Founded by Linus Torvalds in 2005, Git is the most widely used distributed version control system, majorly used for open source projects.
The system permits non-linear development of projects, and handle large amounts of data by storing it on a local server. Although Git gives developers a repository with the entire history of changes, the system comes with some very complicated command syntaxes, that are confusing. As a developer myself, I know how daunting it can be to memorize so many commands. To make things easier for you, I have come up with this Git cheat sheet, along with a glossary of terms to help you out.
Whether you’re a student who needs some assignment help, or a fresher dealing with Git for the first time, help yourself to this quick-glance guide.
Let us start with the Git Glossary, with an overview of all the important terms that you need to deal with on a daily basis.
18 GIT ESSENTIALS THAT YOU NEED TO BE WELL-VERSED WITH
A Git branch represents versions of a repository from the main project. You can keep track of all the experimental changes you make and revert to older versions through a branch log.
A commit command represents a specific point in a project’s history. It helps you save changes you make to the local repository.
The checkout command is used to switch between branches. Make sure to save the commits when you switch between branches.
With the fetch command, you can copy and download branch files to your device.
A fork is basically a copy of a repository. You can use the fork command to experiment with portions of the project without affecting the main assignment.
Head basically represents the most current change saved to the repository you are working with at the moment.
The index is basically the recycle bin of Git where the added, deleted and altered files remain until you commit the changes.
The master is the primary branch of all repositories and includes recent changes and commits.
The merge command is used in conjunction with pull requests to make changes across one branch to another.
The origin refers to the default version of a repository.
Pull represents suggestions for changes to the main branch. You can create pull requests to tell the maintainer to review the changes and merge them to a repository.
The push command is used to update remote branches with the latest changes made.
This option lets you split, move or delete commits.
A remote is same as a branch and are used to communicate upstream with their origin branch.
Git repositories are your memory log that holds all of the files including branches, tags and commits of the project.
Stash removes changes from an index and stores them away for later.
Tags are used to keep track of important commits.
In Git, upstream refers to applying push command on your changes to the master branch.
Now that we are done with the basics, let us explore the various Git command shortcuts!
20+ GIT COMMAND SHORTCUTS THAT YOU MUST KNOW BY HEART
To install Git, you can run this command:
sudo apt-get install git-core
After the download is done, you can run the installation and get ready to use it.
After you install Git, the first thing that you need to do is copy your username and email in the gitconfig file. To access the setup file, you can run this command:
sudo vim ~/.gitconfig
Replace ‘user’ with your username and ‘[email protected]’ with your email and you will be done with setting up.
Create a new directory, you can run this command:
This will create a new repository which consists of three “trees”. The first tree is the Working Directory that has the actual files. The second tress is the Index and acts as the staging area for Git. The third tree is HEAD which serves as a log of the last commit you’ve made.
To check your repository, you can use this command
git clone /path/to/repository
- To add Files and Commits:
You can make changes to the project you are working using the following command:
If you want to add every new commit to the repository, then you can use this command:
git add --all
To check the status of the added files, run this command:
To commit to the changes you make, you can use:
git commit -m "Commit message"
You can use the shorter route, which is:
git commit -a
To connect your repository to a remote server, you can add your commits to it with this command:
git remote add origin < serveraddress >
Now, you will be able to push the changes made to the selected remote server.
To send the changes to the remote repository, run:
git push -u origin master
Branches are used to develop features that are isolated from each other. The master branch is the “default” branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.
To create a new branch (say, we name it “mybranch”) and switch to it using, you can run:
git checkout -b mybranch
To switch back to the master project, you can run this:
git checkout master
If you want to delete a branch, then you can use:
git branch -d mybranch
Others will not be able to access or use your branch if you do not push it to your remote repository. To push it, you can run this command:
git push origin < branchname >
To update your local repository to the latest commit, you can run:
To merge a separate branch into an active branch, say the master branch, you can use:
Before merging to commits or branches, you can preview them by running the following command:
git diff < targetbranch >
You can check the status of the repository and view the history using this command:
To check only those files that you have changed, you can run this command:
git log --name-status
With the above shortcuts, I am sure that Git will be a lot easier for you. Compile your own cheat sheet with the command shortcuts and stick it to your desk, so that you do not have to go back to your books every time you are working on a project on the framework.