Hey! So you are here in this page trying to find/learn something about git! Have you used a source code management system to synchronize your local code remotely before? Do you know that Git is the most powerful SCM. I was convinced and yes it is!
I have actually started SCM with svn( Apache Subversion). In fact started with TortoiseSVN, a GUI tool for Windows. Here there are no commands, no need to remember, so, nothing to worry, Just right click on your web root folder and choose whichever option you need! Sounds easy?
If you want to go with SVN, you can refer these links.
However I would suggest you to try Git! You can ask me why? Well, It’s New! It’s Challenging! And you definitely enjoy it! You can simply follow this to understand working with Git.
Every Git working directory is a full-fledged repository with complete history and full revision tracking capabilities, which is not dependent on network access or a central server.
Git can be really confusing at first when working decentralized. Many questions would be running in your head, like,’ how to start with it?’, ‘how to properly set up the initial repository?’ etc. Assume you do not have the internet and you had some changes that are to be committed, now if its svn, you have to literally copy/paste. But if its git, you can still commit your changes locally using git commit and then push your changes to master using git push origin master. Unlike SVN, yes, Git adds complexity. You heard me right. Git commands like commit vs push, checkout vs clone, all you have to know is which command works locally and which works with server. Still Git seems to be the cool thing. It’s faster than svn.
Git is distributed, SVN is not:
Git like SVN do have centralized repository or server. But, Git is more intended to be used in distributed mode which means, we can actually checkout the code from central repository to our own cloned repository on our machine. Once you made your changes and you do not have internet connection, you will still be able to commit your changes locally, or create a branch locally and commit in that branch. Once your net connection is back, you can then push your changes to the server.
So what do you mean by branch system? Let’s say, I have a project and I want to subdivide my project in 3 parts - design, development & configuration and also assign to 3 different
people(p1,p2,p3). I will create 3 new branches for each person, other than the master branch. Let me take you through the workflow.
To create a branch:
git checkout -b branch1 master -> creates a branch locally with name branch1 as a child of master branch., similarly will create 2 more branches branch2, branch3. Myself p1, had made some changes in my branch branch1.
Git repositories usually are much smaller than Subversions. If you want a dump of code that is already present in Git, you can get the code repository using git clone.
git clone <link-to-code-repository>
You can find the link in the repository page in Github. Make sure you add the link with ssh protocol, instead of https which would require basic authentication(username and password) but ssh tries to use the ssh key.
If your project is all new and you want to share this, you will have to initialize the directory you are working on. Go to the directory and type the following:
This initialises your local repository as git repository and now you will be able to commit the local changes to git.Verify the files which are changed. Do git status for this.
Check the difference of what is added or deleted in each changed file.
git diff -> shows diff of all the changed files.
git diff <filename>” -> shows diff of specified file
git commit -am "commit message"
am - adds and commits the files with message.
To Add the files:
git add .
To add a single file
git add <path-to-file>
To Commit the added files.
git commit -m "commit message"
You want to remove a file/folder that is already added/committed:
git rm -f <filename/path-to-folder>
You want to modify the committed message:
git commit --amend -m "New commit message"
This changes the message of last commit, only if its not yet pushed.
You have committed the changes locally. Now you want to sync all the branches with same code changes. For this you have to push your changes to the server. But before pushing, I want to sync my branch as of master.
git pull remotename branchname”
Eg: git pull origin master -> pulls the master code. Or You can fetch and then merge.
git merge origin master
In this case, we might sometimes get a conflict if there are changes done for same file, on same line number in master and in local. In this case you have to make the changes required to avoid the conflict and commit the files again(git commit -am)
Now, push the local changes:
git push origin branch1
Done!! Simple! These are the basic beginner commands you would need to push your changes.
You wanna play more with git?
Git asks for user credentials each time you try to push your changes or while requesting pull. To avoid this, as said earlier you will have to add the ssh key to your git account.
To check the git user's info.
git config -l
Here are the steps to add ssh key.
In Terminal, Paste the text below, substituting in your GitHub email address.
ssh-keygen -t rsa -b 4096 -C "email@example.com"
This Creates a new ssh key, using the provided email as a label
When you are prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location(/Users/you/.ssh/id_rsa).
Enter passphrase: [Type a passphrase or click enter if you want it as empty]
Enter same passphrase again: [Type passphrase again]
Now the key is generated.
Ensure ssh-agent is enabled:
eval "$(ssh-agent -s)"
Add your SSH key to the ssh-agent.
- Adding the public key to your git account.
- Open /home/you/.ssh/id_rsa.pub copy the key to clipboard
- Login to github, Click the profile photo on top right corner, then click on "Settings"
- In the user settings sidebar, click SSH and GPG keys.
- Click New SSH Key.
- In the title add a descriptive label for the new key, Paste your new key in the "key" field.
- Click Add SSH Key
Done! You will not be irritated by asking credentials anymore.
Sometimes you will be asked to perform one more step, incase there is a maintainer who merges all the branches. Login to git, open the new branch created and create a pull request, So that other branches can pull these changes. -> click on Create Pull Request.
This is the basic flow you would initially need to commit your local changes to server. Few of other commands that you need are:
You have modified a file and now you don't need those changes anymore,
git checkout <path-to-file> resets the file to the last committed version.
Multiple files can be separated by space.
To delete a branch locally
git branch -D <branch-name>
You don’t need any of the changed files, and want to revert to last commit:
git reset --hard
To reset to a particular commit, use following
git reset --hard <commit_number>
eg: git reset --hard 1db6d9af7b6e3b1ebb7e9912ee514e9b50f85af1
To reset to last merge,
git reset --merge ORIG_HEAD
Let’s say you have made some changes to few of the committed files. Now, If you want to save the changes temporarily for single file, may be you will do a copy/paste, but if it's more than one file, it's annoying to copy/paste all the changes.
This reverts all the files to the last committed version saving the changes temporarily.
You want to get back the saved changes,
git stash apply
You want to ignore a file while committing, instead of adding each file, ignoring a single file from the list of changed files would look more better. Also there is no need to avoid this each time. So, for this you have to navigate to the location of your git repository, create a file .gitignore file and list out the the files or directory.
Example: I want to ignore my settings.php file, and a directory test. For this, add the files like below.
You can actually commit this file if you want to ignore any changes blindly,and to share with any other users when they clone the repository.
If you want to do ignore a file locally,
git update-index --assume-unchanged settings.php
To get the last commits:
To get commit details of last n commits.
git log -n
Oh My God!! Too many! Looking Kind of complex? But trust me, Git may be harder to learn, but once you do learn how to use Git, you’ll find it to be feature rich and functional. Git, Do have the GUI as well if you wanna give it a try, like smart git, source tree etc. Of course, Subversion's UI is more mature than Git's. I feel like git is even more reliable. No limits to explore! So you can still go around the web to find more interesting things about Git!