What is Git?
According to its definition on https://git-scm.com
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
You can download git from: https://git-scm.com/downloads
But how do we do that with git? Let me simplify it:
Let's say you are working in a team designing a software to keep track of goods sold on a shop called MyMart. Everyone in your team is working on a separate module of the project.
There is an index.html file which is the homepage of your application.
<html>
<head>
<title> MyMart </title>
</head>
<body>
<!--your changes -->
<div id="welcome">
Welcome to MyMart.
</div>
<!--team-mate's changes -->
<div id="order">
Please order with us!
</div>
</body>
</html>
Now, you are tasked with writing the 'welcome' code, whereas your team-mate is tasked with writing 'order' code.
Now you wrote your code, and so did your team-mate on separate index.html files, how are you going to merge it?
One way is that, you write your code, then your team-mate takes your code, then writes his code on top of your code, but this would be inefficient because your team-mate will have to wait for your code to be finished first. Now let's say your teammate even took your code and wrote his code on top of it, and you deployed the application. But you realize there is an issue with it and your team-mate's code needs to be reverted. In this case, you will have to peruse the code and remove your team-mate's code line by line, which is again very inefficient because what if your team-mate had written thousands of lines instead of just a few?
Here comes Git to the rescue. Now going back to its definition, git is a distributed version control system, let's dissect this term and understand it.
Version Controlling: Version control means tracking changes to your codebase. Like in our MyMart application's case, the first version could have an empty index.html file, then you made your changes and added the 'welcome' message. The job of a version control system is to track these changes, so that you could jump to any change at any time. Here each change that you make is called a commit, and each commit has an id, with the help of this id one can go to any commit, and the project/file will only contain the code that was pushed to it till that commit.
Distributed Version Control: Deriving from the above definition, a Distribute Version Control is a system that tracks changes on each and every developer's machine that has that code. Hence the term distributed. So any developer who is working on the same project, will have all the changes to that project from all the other teammates. But how does this happen? How does one get changes from others' project in their own project? This is done by hosting a remote repository online through services like GitHub, bitbucket, GitLab, etc.
The source code of the project is pushed to this common repository, and all developers copy changes from this repository frequently to keep their local codebases updated. This process is called Taking Pull.
Once, a developer makes changes to their local codebase, and wants others to work on top of those changes, they will have to copy their local changes to the remote repository. This process is called making a Push.
Now let's go through a few of the important git commands, keeping them handy or memorising them is going to help you later. Trust me on this?
Q1. How to initialise a directory as local git repository?
git init
Use this command to initialize a directory as a local git repository. It means that git will start tracking changes from now on in this directory.
Q2. How to add a file to staging area in git?
git add <file-name>
This command adds changed files in the local repo to the staging area. This means, the next time a commit is made, the updates in the file will be pushed to it.
Q3. How to make a commit?
git commit
This will make a commit. Updates from all files in the staging area will be pushed to new commit. You will need to write a commit message after this command, however, there is a shorthand for directly writing a commit message
git commit -m "commit message"
Q4. How to connect a remote repository to my local git repository?
git remote add origin <remote-repo-address>
Once you have created a remote repository, let's say on GitHub, you need to connect your local repository to it so that you can start pushing your changes to it. This command lets you do that.
Q5. I forgot my repository's URL, but I do have the local repository. How can I see the remote repository's URL?
git remote show origin
Q6. What is .gitignore?
.gitignore as the name suggests is a file, which makes git ignore changes to files that are mentioned in it.
Q.7 I have mistakenly committed a file, and now I want to remove it. How can I do that?
git command to remove already committed file
git rm -r /folder/file.jpg
update .gitignore and put entire folder/file.jpg
git add .
git commit -m "removed unwanted files"
Note: if you later realise after committing some files that those shouldn't have been committed in the first place then you would have to first delete those file/folder from git using the above command and then add those files to .gitignore so that they are not pushed again after modification. If you just add those files to .gitignore hoping that from now on these files won't be pushed then that won't work.
Q8. I am working on a feature on my project, a few of my teammates too want to work on the same feature and share code, but we don't want to use main branch for this, what can we do?
In this case, git provides a functionality to create branches from your code. A branch of git is very similar to its etymology, i.e. it is part of the tree, but is splitting from its main trunk. Similarly, creating a branch from already existing ones means it has all the code of the previous branch, but working on this branch would not impact the code on the previous branch and hence can be used for the development and testing of a different feature.
git command to create a new branch
git branch <branch-name>
git checkout <branch-nam>
Shortcut
git checkout -b <new-branch-name>
Q9. What is the command to see the commit history of my repository?
git log
Q10. I have created a branch on my local, but want to push it to a remote repository so that my other teammates and I can together work on that. What is the command to push my newly created local branch to the remote repository?
git push -u origin <branch-name>
Q11. What is the command to rename an already existing branch?
git branch -m <new name>
git push origin -u <new name>
The first command will change the branch name on a local repository. The second command will change the branch name on the remote repository.
Q12. What is the command to delete a branch?
git branch -D <local-branch-name>
git push origin --delete <remote_branch_name>
The first command will delete the branch from your local, the second will delete it from the remote repo.
Q13. My teammate has created a new branch in the remote repository. How can I take a checkout of the newly created remote branch to local?
git checkout --track origin/<new-branch-name>
Q14. I was working on my local project, but have messed up the local code. How can I change my local repository to be exactly like a remote repository?
Reset local branch to remote branch:
git fetch origin
git reset --hard origin/master
Q15. How can I jump to a particular commit and synchronise my project exactly like it was when that commit was made?
First of all use git log command to figure out the hash of the commit you want to jump to then run the following command
git checkout -b <branch-name> <commit-hash>
Q16. How do I keep my feature branch up-to-date with the main branch? What if someone pushes some new code to the main branch, which could impact my feature, then what should I do?
To deal with this, you need to frequently merge changes of master to your feature branch. To do this, first checkout your master branch using
git checkout main
git pull
then checkout your feature branch and merge with main
git checkout <feature-branch-name>
git merge main
Q17. I have made changes to my feature branch, and have tested it too. How can I push those changes to the remote main branch now?
This is a three-part process:
1. Ensuring everything works fine in your local environment
2. Reviewing your code changes
3. Merging code changes to the remote main branch
Let's start with "Ensuring everything works fine in your local environment"
First of all, make sure your feature branch is up-to-date with the master branch and there are no merge conflicts. Once that is done, push your local feature branch changes to remote.
Next, your Repository Host will have a feature to create a "Pull Request". Every version control hosting service like Github, Gitlab etc has this. Create a pull request from your remote feature branch towards your remote master branch. Once you do this, the owner, admin, or any user of that repository has to approve that PR. They should go through the code and make sure you are following all conventions and coding standards and might even point out bugs if you have skipped any.
Once they approve it, you will get an option to merge your PR. Once you do that, your code will be available in remote main branch.
Q18. What is the command to change last commit message?
git commit —amend
This will open commit file, change the message and save it.
Then take a pull of remote branch, it will ask for merge, provide a message and merge it.
Then do a commit. The message will be changed.
Q19. I have made hundreds of commits on my feature branch, but I don't want all of them to be pushed to the main branch. Is there a way to combine these commits into one so that git history is not messy?
Yes, this can be done using rebase by combining multiple commits in one using the following command.
git rebase -i HEAD~<number of commits to go back>
Once you run this command, you will get an instruction window that will give you multiple options, keep following the instructions and you will be able to merge your commits.
Q20. I want my commits on top of everyone else's when I merge my feature branch to the main branch. How do I do that?
To do this, instead of updating your local master with remote using the command
git pull
checkout main branch
git checkout main
then do a pull with rebase flag
git pull --rebase
and then checkout your feature branch and do a merge with main
git merge main
and then push it to remote, once this branch is merged, your changes will be on top.
Congratulations! You are done with git crash course and are now ready to conquer the world of software development.
Happy Coding!
Want more like this?
Subscribe and I’ll send the next post straight to your inbox.
