Git is a specific open-source version control system created by Linus Torvalds in 2005. Specifically, Git is a distributed version control system, which means that the entire codebase and history are available on every developer’s computer, which allows for easy branching and merging. Why use something like Git? Say you and a coworker are both updating pages on the same website. You make your changes, save them, and upload them back to the website. So far, so good. The problem comes when your coworker is working on the same page as you at the same time. One of you is about to have your work overwritten and erased.
A version control application like Git keeps that from happening. You and your coworker can each upload your revisions to the same page, and Git will save two copies. Later, you can merge your changes together without losing any work along the way. You can even revert to an earlier version at any time because Git keeps a “snapshot” of every change ever made.
Terms used while working with Git:
There are a few words I’m going to use repeatedly, none of which I’d heard before I started learning. Here are the big ones:
Command Line: The computer program we use to input Git commands. On a Mac, it’s called Terminal. On a PC, it’s a non-native program that you download when you download Git for the first time (we’ll do that in the next section). In both cases, you type text-based commands, known as prompts, into the screen, instead of using a mouse.
Repository: A directory or storage space where your projects can live. Sometimes GitHub users shorten this to “repo.” It can be local to a folder on your computer, or it can be a storage space on GitHub or another online host. You can keep code files, text files, image files, you name it, inside a repository.
Version Control: Basically, the purpose of Git was designed to serve. When you have a Microsoft Word file, you either overwrite every saved file with a new saves, or you save multiple versions. With Git, you don’t have to. It keeps “snapshots” of every point in time in the project’s history, so you can never lose or overwrite it.
Commit: This is the command that gives Git its power. When you commit, you are taking a “snapshot” of your repository at that point in time, giving you a checkpoint to which you can reevaluate or restore your project to any previous state.
Branch: How do multiple people work on a project at the same time without Git getting them confused? Usually, they “branch off” of the main project with their own versions full of changes they themselves have made. After they’re done, it’s time to “merge” that branch back with the “master,” the main directory of the project.
Here are few commands that I use everyday:(cheatSheet)
Git Init
The git init command is usually the first command you’d run in any new project that is not already a Git repository.
It can be used to convert an existing, un-versioned folder into a Git repository. Also, you can use it to initialize an empty repository.
Using Git Init Command
cd into the directory you want to initialize.
Then, run this command.
git init
This will transform the current directory into a Git repository. A .git sub-directory will be added. This will allow you to start recording multiple versions of your project.
Note: Running git init on an already initialized directory will not override any of your settings.
Git Clone
The git clone command is used to download the source code from a remote repository (like GitHub, Bitbucket, or GitLab).
Using Git Clone Command
git clone <https://url-of-the-repository>
When you clone a repository, the code is automatically downloaded to your local machine.
As a convenience, the downloaded version is linked to the origin (the repository from where you downloaded). I say this is a convenience because when you’re collaborating on the same project, you’d want to push your changes (more on that below) to a single repository.
But sometimes, people wouldn’t want to have this link. If your use case is similar, run
git remote rm origin
This will disassociate the downloaded repository from the origin.
Git Branch
Branch is one of the most important functionalities of Git. This allows teams to work on the same code base in parallel. It enables teams to create Git Workflows and make their workflows more efficient.
Say, you’re working on “Feature A”. And your teammate is working on “Feature B”. By creating a separate branch for each feature, both of you can work on the same code base in parallel without having to worry about conflicts (at least while you’re writing the code).
You can use this command to create a new branch, view existing branches, or delete a branch.
Using Git Branch to create a new branch
git branch <branch-name>
This command will create a new branch only in your local system. If you want this to be visible to all the members in the repository, you’ll still have to push the branch.
To push a newly created branch, run git push -u <remote> <branch-name>
Command to view all branches
git branch
or
git branch --list
Git command to delete a branch
git branch -d <branch-name>
Git Checkout
A mistake I often made when I first started learning git commands was forgetting to switch to the new branch I just created. Yes, after you create a branch, you’ll have to switch to it with another command. That’s where the Git Checkout command comes in.
Using Git Checkout Command
git checkout <branch-name>
This will automatically switch you to the branch name you mentioned in the command.
However, when switching from one branch to another, you need to keep two things in mind:
- If you made some changes in the previous branch, you will have to first commit and push them (I’ll cover this command below) to your remote repository.
- The branch you want to switch must be present in your local system. If not, you can pull them (covered below).
If you’re as lazy as I am, I’m sure you’d want one single command that will both create a new branch and automatically switch to it.
The below command does exactly that.
git checkout -b <branch-name>
Git Add
Every time you create a new file, delete it, or make a change, you’ll have to tell Git to track it and add it to the staging area. Otherwise, the files you made changes to wouldn’t be added when you try to push your changes.
Using Git Add Command
git add <file-name>
This command will add only a single file to your next commit. If you want to add all the files to which changes were made, you can use
git add .
It’s important to remember that using git add will not make any changes in the remote repository. Your changes will be recorded only when you commit them.
Git Commit
Think of Git Commit command like a checkpoint in your development process.
It’s commonly used to save your changes. Maybe after completing a specific work item assigned to you in your agile tool.
Every time you commit your code changes, you’ll also include a message to briefly describe the changes you made. This helps other team members quickly understand what was added, changed, or removed.
Using Git Commit Command
git commit -a
This will commit all the changes in the directory you’re working in. Once this command is run, you’ll be prompted to enter a commit message.
Alternatively, you can enter the commit message in the command itself and skip the additional step where you’ll be prompted to enter the commit message.
To do this, run the following git command:
git commit -m “<commit-message>”
Note: The git commit command saves the changes only in your local repository. It does not push to the remote origin and make your changes accessible for others to collaborate.
Git Push
To make all your committed changes available to your teammates, you’ll have to push them to the remote origin.
Using Git Push Command
git push <remote> <branch-name>
It’s important to remember that git push command will upload only the changes you’ve committed.
Git Pull
Of course, you’d want to have the latest updates from teammates as well!
The git pull command allows you to fetch all the changes that your teammates pushed and automatically merge them into your local repository.
Using Git Pull Command
git pull <remote>
In many cases, you will run into conflict because you had changed a line in a file that another teammate added. In such cases, you need to resolve the conflicts manually.
Git Diff
Git Diff is my go-to command when I want to quickly see the difference between my current branch and another branch (usually the branch I’m merging into).
Using Git Diff Command
git diff
This will show you any uncommitted changes in your local repository.
To compare two branches
git diff branch1..branch2
This will show all the file differences between the two branches.
To compare a file from two branches
git diff branch1 branch2 ./path/to/file.txt
This command will show a comparison of the changes made to file file.txt across the branches branch1 and branch2.
Git Stash
Git Stash temporarily shelves your work, so you can switch to another branch, work on something else, and then come back to this at a later time.
It’s perfect if you need to work on something else and you’re midway through a code change, but aren’t ready to commit the code.
Using Git Stash Save Command
git stash save “<stash-message>”
This will stash your changes with the message you entered. This can be helpful when you want to come back and restore your stash, especially when you have several stashes.
However, this will only stash your tracked files that you added using git add. If you want to include the untracked files as well, run
git stash save -u
Using Git Stash List Command
When you want to view all the stashed code, you can view them using this command. Once you stash your code, git will assign a stash id, so you can restore a specific stashed code later.
For example:
git stash list
This might show the following
stash@{0}: On master: Stashed with message1 stash@{1}: On master: Stashed with message2
Using Git Stash Apply Command
This will automatically restore and apply the topmost stash in the stack.
git stash apply
If you want to restore a specific stash that you want to apply, using the above example, you can simply run git stash apply stash@{1}.
Note: When you use git stash apply, the stashed version will be applied to your current working branch. However, it will not delete the stash from the stack.
Using Git Stash Pop Command
To automatically also delete the stash from the stack, the git stash pop command is used.
If you want to do it for a specific stash in the stack, run git stash pop stash@{0}.
Git Status
When you’re feeling a bit lost with what’s happened in your repository (yes, it can happen) the Git Status command can tell you all the information you’ll need to know.
Using Git Status Command
git status
This can give you information such as:
- Your current branch
- Whether your current branch is up to date
- If there’s anything in the branch that needs to be committed, pushed, or pulled.
- If you have any files that are either staged or not staged.
- And if you have any files that are created, modified, or deleted.
Git Log
While git status gave you nearly all the information you’d have needed, it wouldn’t give you the information about the commit history for the repository.
This is where the git log command comes into the picture.
Using Git Log Command
git log
This displays the entire commit history. If your commit history is large, it’ll show only a portion of it and you can hit [space] to scroll or type q to quit.
- If you want to view only the last 3 commit history, you can use the following command: git log -n 3.
- To condense the commit history into a single line and view them, run git log –oneline. This is the easiest way to get a high level overview of all the commit history. It might still be a bit too much if you’ve got a lot of commits.
- If you want to view the commit history by a specific author, run git log –author”<author-username>”.
Git Merge
Once you’re done with development inside your feature branch and tested your code, you can merge your branch with the parent branch. This could be either a develop branch or a master branch depending on the git workflow you follow.
When running a git merge command, you first need to be on the specific branch that you want to merge with your feature branch.
Let’s see how to use the git merge command with an example.
Using Git Merge Command
Imagine you’re currently in your feature branch called feature1 and you’re ready to merge it to the develop branch.
We must first switch to the develop branch using the checkout command.
git checkout develop
Before merging, you must make sure that you update your local develop branch. This is important because your teammates might’ve merged into the develop branch while you were working on your feature. We do this by running the pull command.
git pull
If there are no conflicts while pulling the updates, you can finally merge your feature branch into the develop branch.
We do this by using the git merge command followed by the branch name that we want to merge into our current branch.
git merge feature
Happy Coding ..