An open-source software used to track changes in files and help developers collaborate.
Before understanding the basic operations in GIT let’s understand the various stages.
Working Directory(Learning Area): Assume there is no GIT and you are working on a source code. This represents that. Normal files. You do all your dirty work here. You know the never-ending cycle of undo and redo :)
Staging Area(index): Assume you have created a lot of new files, deleted a bunch, and modified and handle full of them. But you are not sure if that’s final. Staging is is a proposal to GIT about what might come in the next commit. For you, it’s a safe place where you keep your code before being confident enough to commit. Adding or removing files from staging has no bearing on the versioning of a file.
Local Repository: Now we are talking versioning. The versioning maintained in your local computer is the Local repository. Files inside this maintain versioning but only you can see it. It is not in the cloud yet!
Remote Repository: All the versions present in your local repository can be pushed to the cloud or remote repository for other collaborators to be able to see it.
git init — Starting a new project which will have versioning in local git.
git add — Every time you create a new file, it only resides in your workspace/working directory. GIT has no awareness of this file. These are called untracked files. add command pushes the file to the staging area, preparing GIT to accept this file in the next commit.
git rm — Remove files from the GIT repository as well as the working directory.
git commit — Done with all your changes? Added all untracked files? Time to commit the changes to the local repository using git commit. While committing it is best practice to provide a comment to let others know why you made those changes. This can be done using git commit -m “your comment”
git push — Once the changes are in your local git repository you can push it to a remote repository in the cloud using the push command. You need not execute a push after each commit. You can have as many commits as you want in your local repository and only trigger a push when your changes are ready or required for other collaborators to see. push will send all commits from the last push to the cloud(remote repository).
In all of the above commands, you were making changes to the repository. Now assume another developer is working on the same remote repository and has pushed few changes. How will you get the update or collaborate? The below set of commands helps you with that.
git fetch — gets up-to-date data from remote to the local repository. From the diagram, you can see that the changes are only available in the local repository and not in the working directory. This means while your local git repository is aware of all the changes available in remote, it has not merged the changes into your working directory. This is helpful in understanding what to expect when you actually pull the data into the working directory.
Most modern/popular IDEs have an auto fetch running in the background. This helps it show you how many new changes are pending to be pulled into your working directory.
git merge — merging changes that you fetched in the previous step into your working directory or in other words making it visible in your working directory.
git pull — Shortcut for above 2 steps (fetch and merge). When you are really really confident that there won’t be conflicts :)
git checkout — Assuming you understand the idea of branches, your remote and local repository would have many branches, and your working directory at any given point of time only shows or points to one branch. checkout command helps you change the current branch you are pointing to or want to work on.
git diff — Helps you compare differences or changes you have made in workspace or working directory with the repository.
All popular IDEs have GIT integerated which makes running these commands eaiser with the help if GUI. However, it’s still important to understand the concepts behind these commands. Oversimplified series just gives you a peek, further study and hands on is essential.