Let’s Git Familiar — Part 1: Git Fundamentals
This story is the first part of a series on Git Version Control System Fundamentals. If you want to proceed, you can find the second part of the series here.
Git is a free, open-source Distributed Version Control System (DVCS) and it keeps track of projects and files as they change over time with the help of different contributors. Git keeps track of the changes made to the code. So when a bug arrises you can see what stage of the development caused the bug and you can revert the change.
The major difference between Git and any other VCS’s like SVN is the way Git stores the data parts. Git doesn’t store the data like a regular file system. With every commit, Git captures the current Snapshot of the files and stores them. To be more efficient, if files have not changed, Git doesn’t store the file again, just a link to the previous identical file it has already stored.
This part is going to be only an introductory to Git ecosystem, the details of how Git works and all of the workflows will be mentioned in later writings. So now let’s go over the Git commands and how you use them briefly. By the way you surely can use any of the Git GUI application to handle Git commands but I strongly suggest you to at least learn the commands on terminal.
Almost every Git operation is done on the local machine. (That is one reason why Git is super efficient. The performans details will be on the later writings.)
Let’s review the basic commands that you will need when you start to use Git.
So let’s assume that you are working on a project. You created the bare project and wrote some code. Now it is time for you to use some Git magic. Open up your terminal and go to your project’s location.
First command is the initialization command. With it, you create your local Git repository and a file with .init extension is created.
After that you can add your personel information to that git repository such as your e-mail address and your user name. (These are optional of course.)
Git config —global user.name ‘your user name’Git config —global user.email ‘your e-mail address’
Important stuff! You add your files to your local git repository using git add <file name> command. Ex git add index.html If you want to add all files in your directory you can use git add . For example if you only want to add your html files to your local git repository you go with git add *.html
git add index.html // adds index.html to local git repogit add *.html // adds all html files to local git repogit add . // adds all files to local git repo
Now you keep track of every change you made on your local repository using git status command. It show you which files you added and which files you did not. It also notifies you if you made some changes on any added files. This command is your best buddy. You will use it a lot to know what is going on with the local repository.
You used git status and you have seen some files you did not want to add to your local Git repository in the first place. You can undo that by git rm — cached <file name>
git rm --cached index.html // deletes index.html from git repo
You often have some files you don’t need to version with Git and you don’t want to manually add or delete them over and over again. All you need to do is you create “.gitignore” file.
Let’s examine this with and example. You have a log file called “Log.txt”. It is very common not to version log files so you don’t want to add it to your repository. All you need to do is to open your .gitignore file and write one line: Log.txt . After that git add . adds all of your files to your local repository but “Log.txt”. Git excludes every file or directory in .gitignore file. (You place / before your directory name.)
This is the time for taking snapshots of your files on your local repository. Git commit does that for you. Write git commit -m ‘<commit description goes here>’ on your terminal and you get your first snapshot of your code files. (Or any other file for that matter.) After that whenever you make any changes on your files, you first write git add . To add all of them to local repository and use git commit -m ‘<description>’ to commit those changes.
git commit -m 'initial commit'
So far you commit your changes but all of your files are still in your computer. You need to send them to a remote server for obvious reasons. Let’s say you or your team is using Github for Git VCS. For one time only you need to add your remote repository to your project. With git remote add origin https://github.com/user-name/project-name you add your GitHub repository as remote repository for your project. After that all you need to do to send your files to the remote repository with git push command. It will push your local repository to Master branch of your remote repository. (No worries. Branching will be on the later writings.)
git remote add origin https://github.com/myUser/myAwesomeApp
You also get your projects latest version by writing git pull via your terminal. If you want to get a project from Github or any other Git based VCS servers, you use git clone <git url of the project or ssh link>
git pull // Gets the latest snapshot from remote git repo to your // local repogit clone https://github.com/myUser/myAwesomeApp.git // Clones the // app to your directory
This story has became very long just with these basic commands. You can find more advance commands like branching, merging, creating pull requests, rebasing etc on later writings. You can also find information about some best practices about Git version control system such as Git Workflows on later writings as well. You can find the second part of the series here.