A quick hands-on introduction to Git
What is Git?
Git is a distributed source control system. Now what it means is that every Git directory on every computer is a complete repository with full history and version-tracking abilities, not dependent on the network/internet or central server.
Why should we use Git?
Git is simpler, faster and easier to use than traditional “centralised” source control systems. For those pre source control ages, we have worked on backing up our files important files before editing to keep track of all the changes or not lose them. That would look something like this for example: accounts_v1.txt, accounts_orignal.txt, accounts_old.txt, accounts_new.txt etc. That’s where the Version Control System like Git comes into play. These tools help you to mange the code by keeping track of your files and you can see what was merged, what was added, what was deleted and so on. In other words, familiar with undo/redo? Git does that but for every change ever made by any of your colleague on the project.
Git vs Github/GitLab/BitBucket/TFS
Think of Git as a “website” and Github/GitLab/BitBucket/TFS as the “hosting provider” for this website. You can use any hosting provider, all implement the git features in the same way. Github/GitLab/BitBucket/TFS allow you to easily work with git, view source code, create pull requests, consolidate changes etc. and have your source code on the “remote” so it is safe in case something happens to your main development machine.
Getting started with Git
To install git on your machine, i highly recommend using chocolatey (more on that here)
choco install git.install -y
Open up PowerShell, it’s time to setup git. We need to setup the github username and email. This will show up in your git commit details on GitHub. To add a user email and username to the config:
git config --global user.name "[YourGitHubUsername]" git config --global user.email "[YourGitHubEmail]"
Notice we are passing –global parameter to save these config for every git repository. To check your username and email type:
git config user.name git config user.email
Now to initialise git repository in the current folder:
This will create a .git folder in the current folder. This is called git repository. Git repository is a complete history of changes on your codebase over time.
Add files to staging area once they are ready to be committed.
git add .
We will not necessarily add all files to staging area because it is not recommended to commit build and generated files to the repository. These could be your dll/exe and other config files generated by your IDE. To ignore these files we simply place a .gitignore file in the root of the repository and include all the files/folders in there which we don’t want to be tracked by git. A link is provided at the end of this post with a comprehensive repository of .gitignore files for almost every project type.
Type the message of your commit, a message should be comprehensive enough to let the team members (or even yourself know after couple of months) know what changes have been made to the code base and what is expected.
git commit -m "commit message"
Set your local repository to the connect to remote repository hosted on GitHub.
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git
Push your changes
git push --set-upstream origin master git push --set-upstream origin/feature-branch-1 feature-branch-1 # in case to push your new feature branch 1 which does not exist on remote yet.
Some other useful Git commands:
git checkout branch-name # will checkout to another existing branch. git checkout -b branch-name # will create the new branch and will switch to it. git branch -m new-branch-name # to rename an existing local branch which hasn't been pushed to remote yet. git branch -d branch-name # delete a branch locally git log -n # gives you the list of commits in the branch history (where n is a number of commits you want to see) git stash # putting your current changes on a side for the moment and going back to original state of your branch. git stash apply # will apply the changes you stashed earlier to current branch (but will keep the copy in stash) git stash pop # will apply the changes you stashed earlier to current branch and will delete the copy from stash
https://git-scm.com/ – Official Git documentation
https://learngitbranching.js.org/ – Learn Git branching by playing game.
https://github.com/github/gitignore – Comprehensive source of .gitignore files for every project type.
That’s all folks, happy coding.