A friend requested to do a small hands on workshop on git. I prepared the following very brief introduction and tutorial for him and his team.
What is Git?
Git is a distributed revision control and source code management (SCM) system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development in 2005.
What are forks?
When you are cloning a git repo on your local workstation, you cannot contribute back to the upstream repo unless you are explicitly declared as contributor.
So that clone (to your local workstation) isn’t a fork. It is just a clone.
Basic Git Operations
To initialize an existing project in Git, go to that directory and type this command.
1 |
git init |
In order to work with code, you will get a copy of it. The path to clone could be copied from bitbucket / github etc shown at top e.g. git clone https://github.com/mrkkhattak/ mysql-scripts.git
1 |
git clone |
Once you have made changes to the code, before committing doing git status will show a list of modified files for analysis.
1 |
git status |
If there are any files, you don’t want to include in your project on git create a file named .gitignore with names of those files e.g. out put of a .gitignore file:
1 2 3 |
$ cat .gitignore *.var.log *~ |
In git you have to add files / folders which are not yet part of project before you could commit them. To add an individual file git add filename (this concept is also called staging in git)
1 |
git add |
To add all modified and deleted files in your project, run this command:
1 |
git add -A |
This records a snapshot of your changes. With every commit it is recommended to include a comment so in future it could be easily tracked and know what changes were done and why.
1 2 3 |
git commit git commit -m"Updated functions.php for new references." |
The commits don’t push the changes immediately to the remote repository. To push them to remote repository, use this command:
1 |
git push |
To see the differences between between your changes (not yet committed or pushed) and your last changes already committed run this command:
1 |
git diff |
To remove a file in git first you remove the file from local, then run the following command, which will communicate this change to staging. In next commit, it will remove this file completely from repository as well.
1 |
git rm |
To rename a file, use this command.
1 |
git mv |
By default, with no arguments, git log lists the commits made in that repository in reverse chronological order. There is a comprehensive list of options available to utilize with this git log command to make it more useful. Some common scenarios are comparison of history word by word, line by line etc.
1 |
git log |
If changes have been pushed to your fork by other users, you will need to pull them in before you can push. This pulls those changes in and applies your changes on top of them.
1 |
git pull -rebase |
Above I have listed only a few common scenarios / commands, while git provides many more options and it will take time to master them all.
There are many resources available which would help in learning and exploring git further. Following are the two links which I found pretty useful: