An Introduction to Git

A three part article series on version control using Git and GitHub.

Photo by Yancy Min on Unsplash

This is the first article in the series in which I will give a very brief introduction to Git. This will allow most readers to understand enough to utilize it for version control during development.

Lets start with some definitions. Git is a source control software allowing you to take snapshots and distribute your creations and modifications over time.

  • Terminal (for Unix or Mac) or Command Prompt for Windows allows us to type Git commands and manage project repositories. In this section we will be focusing on terminal commands.
  • By default we are in the /home/vivek directory. home and mnt folders are in the same directory (usually they are in the highest level directory signified by just a /)
  • pwd shows the current directory
  • clear is used to clear the command line
  • cd + tab key is used to cycle between sub directories in a directory
  • cd .. is used to move up a directory
  • cd mnt/ is used to enter the mnt directory. In this directory we can find the windows c drive (basically it is a directory named c)
  • ~ signifies that you are in your home directory
  • .. is used to move up one directory
  • / signifies the highest level directory, you cant go back from there
  • mkdir is used to create a new directory
  • Directory names are case sensitive
  • Right click is used to paste an absolute path name in the terminal
  • ls is used to list all directories and files in a directory
  • rm -rf is used to remove folders. rf tells that we are using the command to remove a directory, as by default rm is used to remove a file
  • git --version is used to see the version of git
  • touch file_name.txt is used to create a file
  • Git Repository is used to save project files and the information about the changes in the project. Repository can be created locally, or it can be a clone Git repository which is a copy of a remote Git repo.
  • git init is used to initialize the directory as a git repository. This will create a .git folder in the directory and we can start using git features
  • git status shows staging area. You will see some files under “Untracked files:” header
  • git add file-name is used to add a file to staging area. After this you will see the file under the “Changes to be committed:” header
  • git add . is used to add all files in directory to staging area (. signifies all)
  • git rm --cached file.txt is used to unstage a file
  • git rm -f file.txt is used to force remove a file from staging area and also deletes the file from directory (-f signifies force)
  • git config --global “”
  • git config --global “”
  • git commit --help
  • git commit -a -m “Initial commit” (-m to include a message; -a to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected)
  • git log (if you want to see a shorter version then use git log --oneline)
  • Head is usually on master (most recent commit). Head is what the project directory looks like.
  • git checkout <commit-id> is used to see the contents of the folder as they looked during that particular commit
  • git checkout master is used to restore the head to the most recent commit, hence the contents of the project directory are also restored to what they were at the time of the most recent commit
  • git revert <commit-id> is used to revert the contents of the project directory to what they were before that particular commit. This will still appear in the log and we can go back to that commit by using git revert again
  • git reset — three kinds — soft (only goes back in time in the commit tree, so just moves the head back; this is similar to checkout), mixed (moving back in time in the project directory but still can come back, doesn’t remove files) and hard (moving back in time in the project directory and staying there, removes files)
  • touch .gitignore, now open the .gitignore file with notepad and add the names of the files you don’t want to track in that. # can be used to comment in this file. Usually you create .gitignore during initializing the project. If you have committed files already before adding them into the .gitignore file, then you need to remove them from cache by using the following series of commands: git rm -r --cached . then git add . then git commit -m “message”
  • If there is a directory in your project folder and you want to ignore all files in the directory from future commits, you can add “directory-name/*” in the .gitignore file
  • Lets say there is an error in one of the files in the project folder
  • We can create a branch to fix the error while the master repository stays intact
  • git checkout -b err01 (creates a new branch called err01)
  • <fix the error in one of the files in the project folder>
  • git add . (add all changes made to the err01 to the staging area, so they can be committed)
  • git commit -m ‘fixed error’ (commit all changes made to err01 branch)
  • git checkout master (switch back to master branch)
  • git merge err01 (merge changes made in err01 to master branch; merging will only take last commit of err01 and weave it into the master branch commit timeline)
  • git push (this will push master branch of project folder to remote repository)
  • git push origin err01 (this will push err01 branch of project folder to remote repository)
  • git push origin --delete err01 (we delete the err01 branch as we don’t need it anymore). Alternate way: git branch -d bugs (local branches can be deleted using -d)
  • git branch -a (list all branches)
  • First step is to create a new repository on GitHub (don’t add a read-me, gitignore or license). Copy the url of the repository
  • Create a project folder in your local machine and browse into that folder using bash
  • git init (you will see that the repository has not been initialized yet; git init is used to create a new repository)
  • git remote add origin <paste url here>
  • git remote -v (you will see that the repository has been initialized)
  • In GitHub website we create three new files: We click “Create new file” and then create, LICENSE, .gitignore. In content of .gitignore file type /AutoGen to exclude all files that we keep in that folder
  • pull — go back to bash. git pull origin master (we don’t need to specify origin master is we set master as the tracked branch). git branch --set-upstream-to=origin/<branch> master. Sometimes you might be prompted for a login at this stage
  • <make changes to the local repository>
  • git push -u origin master (push updates to remote repository on GitHub; will ask for username and password)
  • You can add other developers as collaborators to this repository.

Comments welcome!

Experienced in synthesizing data to identify trends, deliver insights and recommendations. Focused on customer lifecycle, cross-sell, and employee performance.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store