Some people always run away from version control but its hard truth that it’s not your enemy but friend and it cannot be neglected. You’ll have to use it in some of your bigger projects. That’s why it is necessary to understand its basics, how it works and where you need to use which command.
You’ll have to initialize git using `git init` command in your project or clone some repository using `git clone`
Order in which you execute git commands
git status
This command will display list of files that you have changed at this time you can spot any files which you don’t want to push and can revert or ignore them. But its normally good practice to see before what your going to push.
git add .
After making any changes to the code this can be your first or 2nd command to execute as it’ll add all files to the commit list that you’ve changed, added or deleted.
git commit -m “Meaningful name of your commit”
At this point your should give a meaningful name to your commit using the command above. Its always better to include the task number at start of commit on which you are working followed by some meaningful name. Examples of good commit name are “Ticket-1112: Fixed redirect issue”, “Fixed a fatal error on product page”. Now the examples of bad commit names “Testing commit”, “Fixed”, “Done”, because by seeing these commits nobody would know what you did in that commit.
git pull
Git pull is extremely important to run on this stage because it’ll fetch other developers changes to your local machine. And this is the point in which `conflicts` can arise and you’ll have to fix them here. If you see any conflicts in any file when git generates the message, you’ll now have to fix that conflict.
The conflicts can be fixed by opening the file which has conflicts then you’ll have to search through file for code in between <<<<<<<<< and >>>>>>>>>>. And there you’ll have to decide which code will remain and which will go. In case your don’t know what should remain and what should go its always better to ask from your peers for assistance though. But this step is important as your wrong choice at this point may remove the new fix of the other developer, so its always better to be sure what should be removed and what should be kept.
After fixing the conflict you’ll again have to run thegit add .
,git commit -m "Fixed conflict"
,git pull
git push
This should be your final command in case somebody else doesn’t push code before you 🙂 If that happens git will let you know and you won’t have to worry about that. You’ll just have to execute git pull
first followed by git push
.