The Git Cheat Sheet
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
Create or clone a repository on the current directory.
- 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: git showViewing A Specific Commit:
git show <SHA of commit>
“Staging” means moving a file from the Working Directory to the Staging Index.
Take files from the Staging Index and save them in the repository.
- Commit staged files:
This command will open the code editor. Inside the code editor you must supply a commit message, save the file and close the editor.
- 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.
is 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.
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.
- 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.
- Modify last commit message:
git commit --amend
Revert a commit
This will create a new commit that reverts or undos a previous commit.
Reset a commit
This will erase commits.
Depending on the flag you add you’ll obtain a different result:
flag to erase commits
flag to move the committed changes to the staging index
flag 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!
Previously published at https://medium.com/@elsascola/the-git-cheat-sheet-5858865457ef
Subscribe to get your daily round-up of top tech stories!