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:
Create or clone a repository on the current directory.
git clone <https://github.com/>...
git clone <https://github.com/>... new_name
git log --oneline
git log --stat
git log -p
git log -p -w
git show <SHA of commit>
“Staging” means moving a file from the Working Directory to the Staging Index.
git add <file1> <file2> … <fileN>
git rm --cached <file>...
git add .
Take files from the Staging Index and save them in the repository.
This command will open the code editor. Inside the code editor you must supply a commit message, save the file and close the editor.
git commit -m "Commit message"
Tags are used as markers on specific commits. These are really useful to assign a version to the code.
git tag -a <tag(v1.0)>
git tag -a <tag(v1.0)> <SHA of commit>
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.
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.
git branch <branch-name>
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.
git branch -D <branch-name>
git checkout <branch-name>
git branch <branch-name> <SHA of commit>
git branch <branch-name> master
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.
git merge <branch to merge in>
git commit --amend
Revert a commit
This will create a new commit that reverts or undos a previous commit.
git revert <SHA of commit>
Reset a commit
This will erase commits.
git reset <reference to commit>
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