Getting Started with GIT

I recently created these flashcards to help with learning GIT. This is just a quick follow up post to cover some GIT basics.

Version control, also known as revision control or source control is the management of changes to documents, code, and other collections of information. Changes are usually    identified by a number or letter code, termed the “revision number“, “revision level”, or simply “revision”. For example, an initial set of files is “revision 1”. When the first  change is made, the resulting set is “revision 2”, and so on. Each revision is associated with a time stamp and the person making the change. Revisions can be compared, restored, and with some types of files, merged.

GIT is far and away the most widely used version control system in use today. GIT is an open source project, developed in 2005, by Linus Torvalds. GIT has a distributed architecture, so is known as a DVCS – Distributed Version Control System.

Setting up the GIT environment:
git config user.name – set the user name to be used for all commits for the repo

Basic Commands for using GIT:
git init – creates an empty git repo in the current directory
git clone – clone a repo – this could be a local repo or a remote repo – via ssh or https
git add – stage the changes in the directory for the next commit
git commit – commit the staged files – opens a text editor for you to add a comment to the commit
git commit – m – commit the staged files, adding a message during the commit
git status – lists the status of the files in the directory – e.g. staged, un-staged, tracked
git log – display the commit history for the repo

Undoing changes to the repo:
git revert – reverts changes by creating a new commit that undoes the changes made in previous commit

Branches
The default branch is the master branch. You can create more branches which allow you to make changes to the files in your repo without overwriting the ones in your master branch. This is useful when making changes to your code, which you can then test before committing to your master branch.

git branch – lists all of the branches in your repo

# checkout means to switch focus to another branch in the repo!
git checkout -b – create and checkout a new branch
git checkout – checkout an existing branch
git merge – merge a branch into the current branch

Remote Repos
Often when working with git, you will be working with a remote repository, e.g. github. or a privately hosted gitlab server. There are a number of git commands for working with remote repos

git remote add – adds a connection to a remote repo
git fetch – fetches a chosen branch from the remote repository
git pull – pulls a copy of the remotes copy of the current branch, which is merged with the local copy
git push – push the current branch to the remote repository. This also creates the branch in the remote repository if it doesn’t already exist

Diff
Use diff to do comparisons
git diff HEAD – displays differences between your working directory and the most recent commit
git diff –cached – displays differences between staged changes and the most recent commit

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More