Version control is integral in todays Software Engineering world. There are different version control software’s like Mercurial, Git, SVN. From my experience I’ve worked with Git, Mercurial and CVS. All these tools helps solve one major problem: Maintaining and Managing your development source code. All software companies use some kind of version control and it is essential to understand the concepts and learn how to wield this tool to our advantage.
Note: I’m writing this article to serve as a reference material for students who attend my Workshop on Git and GitHub on July 28, 2021. Other’s can use this article as a cheatsheet for Git commands. Assuming that whoever lands here knows the use and importance of version control, I’m not going to spend more time convincing why you should use a version control system, and dive straight into the content.\
Git vs GitHub
Git is a version control system, it helps to track and manage our source code.
GitHub is a cloud based web-portal which lets us store our Git repositories.
GitLab, BitBucket are some other hosting services like GitHub that lets us manage our Git Repositories.
Install and Configuring Git
Step 1: Download and Install Git from the following URL: https://git-scm.com/downloads
After installing the Git in the local machine, initial configurations has to be completed so we can start using it.
Step 2: Open command line and run the following commands
git config --global user.name "[FullName]"
git config --global user.email "[Email]"
Now Git has been successfully configured, and it is ready to be used in the projects for Version control.
[Optional] Installing GitHub Desktop
Step 1: Download and Install Git from the following URL: https://desktop.github.com/
Step 2: Login with your GitHub credentials in the GitHub Desktop to complete configuration.
Some people feel GitHub Desktop provides relatively better GUI experience than Git’s default GUI tool. (I prefer using Git Bash over GUI 😋.)
Initialize a Git Repository
In simple terms, “Git Repository” is a Git enabled Directory/Folder. Git keeps track of changes in this repository. Any directory can be made into a Git repository by initializing Git in that path.
To initialize git, navigate to the directory in the command line and execute the following command:
git init
Clone a Git repository
In case if the Git repository already exist in some central hosting server, and you need a copy of the repository to work locally, you can clone the repository in your end.
git clone [URL]
Replace [URL] with the URL of the remote repository.
Example: git clone https://github.com/laxmena/100MLProjects.git
will create a copy of my repository in your local system.
How do you find the URL of the repository? If you are using GitHub to host your Git repositories, check the screenshot below to find the URL of your repository.
Making your first commit
“Commit” creates a snapshot. i.e. Your progress so far is saved and you can revisit this point anytime without having to worry about losing your progress.
Making a commit is a two step process. First you need to stage the changes that you need to commit, and next you commit your changes.
Step 1: Stage changes. Staging is done by using ‘git add’ command.
git add [File1, File2, ..]
or if you wish to add all your changes so far, you can use
git add -A
Other flags that you can use:
git add .
: stages new files and modifications without deletions
git add -u
: stages modifications and deletions without new files
Step 2: Commit your changes
git commit -m [Commit message within quotes]
Eg. git commit -m "Create new roles"
Tracking and Comparing changes
git status
: This command shows “staged” modified files in the repository
git reset [File]
: If you want to unstage a particular file, you can use this command to demote the status from staged.
git diff
: Shows diff of the modified files(that are not staged).
git diff --staged
: Shows diff of the modified files that are staged but not committed.
git diff <commit1> <commit2>
: Shows difference between two commits
Know more about git diff here: https://git-scm.com/docs/git-diff
Working with Branch and Tags
A Git Branch represents an independent line of development.
git branch
: Lists available branches
git branch [branch-name]
: Creates a new branch with the specified name
git branch -d [branch-name]
: Delete the branch
git checkout [branch-name]
: Switch to a different branch
git merge [branch-name]
: Merges ”[branch-name]” branch with the current branch (combines changes between branches)
git tag
- add tags to specific branches
git tag -a v1.0
: (adds a new tag, flag ‘a’ represents annotated)
git tag -d v1.0
:(deletes tag v1.0, flag ‘d’ represents delete)
Logging and Status
git log
: Displays info about the previous commits
git log --oneline
: Displays SHA and Description in a single line
git log --stat
: Displays modified files, and summary of changes in the files
git log -p
: Display actual changes made the the files
git log -w
: ignore whitespace changes
git log -p [SHA]
: View a specific commit
git show
: Display info about existing commit
git show [SHA]
: Show a specific commit
Amending a Commit Message
git commit --amend -m "[Message goes here]"
: Amends the previous commit with new message.
Useful Command-line commands:
ls
- List files in the current directory (Linux/Mac)
dir
- List files in the current directory (Windows)
mkdir
- Make new directory
cd
- Change current working directory
rm
- remove/delete files and directories
pwd
- print working directory (Linux/Mac)
cwd
- print current working directory (Windows)
mv
- move file/director from one path to another
My Favorite VS Code Extension: GitLens
You can download the VS Code Extension GitLens and it can be very very useful.
You can find the extension here: https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens
Creating a Profile README for your GitHub profile:
We have been taught not to judge a book by its cover, but unfortunately many do that. You can make use of the Profile README feature that GitHub provides to make your best impression to those who view your GitHub profile.
I’ve previously written a step by step guide of how to setup GitHub profile README here:
Part 1: Creating a GitHub profile README for students
Part 2: OpenSource Widgets to Power GitHub Profile README - Part2
Keep watching my website for more articles related to Software Engineering, Technology, Machine Learning, Books and other useful information. You can signup for my Bi-weekly newsletter to get notified about my recent articles. You can find the signup form at the end of this article.
If you have any questions, suggestions or feedback, you can contact me through LinkedIn and Email.