Git is one of the most popular Version Control Systems out there, you can think of it as a way to take snapshots (commits in Git nomenclature) of your code in a specific state and time, just in case you mess things up later and want to go back to a stable version of your code. It’s also a great way to collaborate if you combine it with GitHub.
Git is free and open source. You can download it from the official website. Once it’s installed you should be able to run Git commands on your terminal.
A couple of things you should know before starting to use Git:
- When you want to keep track of the files in a project you put them inside a repository. A repository is basically a directory in which version control is enabled and always vigilant of what you put in there. It will know whenever any changes are made to those files, and it will help you keep track of them.
- Each commit has an identifier in case you need to reference it later, this ID is called SHA and it’s as string of characters.
- A working directory contains all the files you see on your project directory.
- The staging index is a file in the Git directory that stores the information about what is going to be included in your next commit.
Create or Clone a repository
Create or clone a repository on the current directory.
- Create repository from scratch:
- Clone an existing repository:
git clone <https://github.com/>...
- Clone repository and use different name:
git clone <https://github.com/>... new_name
- Determine a Repo’s status:
- Display a Repo’s commits:
- Display a Repo’s commits in a compact way:
git log --oneline
- Viewing Modified Files:
git log --stat
- Viewing file changes:
git log -p
- Viewing file changes ignoring whitespace changes:
git log -p -w
- Viewing most recent Commit:
- Viewing A Specific Commit:
git show <SHA of commit>
“Staging” means moving a file from the Working Directory to the Staging Index.
- Staging Files:
git add <file1> <file2> … <fileN>
- Unstaging files:
git rm --cached <file>...
- Stage all the files:
git add .
Take files from the Staging Index and save them in the repository.
- Commit staged files:
git commitThis command will open the code editor. Inside the code editor you must supply a commit message, save the file and close the editor.
- Commit files without opening the code editor:
git commit -m "Commit message"
- See changes that have been made but haven’t been committed yet:
Tags are used as markers on specific commits. These are really useful to assign a version to the code.
- Add tag to the most recent commit:
git tag -a <tag(v1.0)>
- Add tag to specific commit:
git tag -a <tag(v1.0)> <SHA of commit>
-ais used to create an annotated tag, which includes extra information such as the date of creation and the person who made it. It is usually considered a good practice to add this flag.
- Display all tags in the repository:
- Deleting A Tag:
git tag -d <tag-name>
When a commit is made in a repository it’s added to the branch you’re currently on. By default a repository has a branch called master. Specially when experimenting with new features on your code, it is often useful to create a separate branch that acts as a safe isolated environment from your last commit. You can switch between branches and the commits will only be added to the one you’re currently on.
- List all branches:
- Create new branch:
git branch <branch-name>
- Delete branch:
git branch -d <branch-name>You cannot delete a branch you’re currently on. You cannot delete a branch if it contains any commits that aren’t on any other branch.
- To force deletion:
git branch -D <branch-name>
- To force deletion:
- Switch between branches:
git checkout <branch-name>
- Add branch on a specific commit:
git branch <branch-name> <SHA of commit>
- Start branch at the same location as the master branch:
git branch <branch-name> master
- See all banches at once:
git log --oneline --decorate --graph --all
When a merge is performed, the other branch’s changes are brought into the branch that’s currently checked out.
- Perform merge:
git merge <branch to merge in>
- Modify last commit message:
git commit --amend
Revert a commit
This will create a new commit that reverts or undos a previous commit.
- Undo the changes made in a commit:
git revert <SHA of commit>
Reset a commit
This will erase commits.
- Reset commit:
git reset <reference to commit>
Depending on the flag you add you’ll obtain a different result:
--hardflag to erase commits
--softflag to move the committed changes to the staging index
--mixedflag to unstage committed changes
If you want to learn more about Git, Udacity offers this great course that covers all the basics and many more concepts in depth (for free!). I did this course myself and it served as an inspiration to make this summary about some of the main concepts I learned and now use in my projects.
Hope you got some value from this post 😊 see you in the next one!