Source version control with Git
-
Git is a popular distributed version control system.
-
Git records changes to your source code over time allowing you to recall specific versions later.
-
Users work on their local copies of the source code.
-
A repo is just a directory of files. Deleting a repo = simply deleting that directory.
Creating a local repository
- Copying an existing repo to a local device is called
clone
ing,
Simple workflow
-
Changes are
Add
ed to get them to the "staging area" -
Staged changes are then
commit
ed to the local repo. -
local repo is then
push
ed to the remote repo. -
Now your changes are kept track in your local repo and remote repo.
- The above workflow works for a single developer keeping their changes tracked. What happens when another developer committed and push their changes to the remote repo while you were away?
Updating local environment with latest version of the code
There are several ways of doing this:
git pull
which does a combination of 2 steps in order to get latest version of code to your working space.-
git fetch
which updates the local repo with content from remote repo (your working directory still does not have the latest remote changes) -
git merge
merges the local repo with your working directory.
-
Alternatively,
git pull --rebase
does a fetch + rebase wherein, your local commits are replayed and commit history is preserved.
Viewing a log of all commits
-
git log
shows a commit log of all changes along with their hashes. -
git log --oneline
is more useful as it is concise and easy to copy commit hashes from.
Reverting a change
-
So you realize that a commit you made was a mistake, and it needs to be undone. After all, this is the benefit of using a version control system in the first place.
-
First do a
git log --oneline
and identify the commit you want to undo, and copy its commit hash. -
Use
git revert <commit hash id>
to undo a specific commit. Remember that it made this change only to your local repo. If you want to propagate your change(undo) to the remote repo, you still need to do agit push
.
To Do:
- Branch - What is? How to create a new branch? Switching to it? Merging with master? What happens when merging?
- Conflicts - Example, how to resolve?
- Stashing? What is ?
- Cherry picking? What is?
References:
- https://rogerdudler.github.io/git-guide/
- https://rachelcarmena.github.io/2018/12/12/how-to-teach-git.html
- https://dev.to/unseenwizzard/learn-git-concepts-not-commands-4gjc
- https://git-scm.com/book/en/v2
- https://ohshitgit.com
- https://csswizardry.com/2017/05/little-things-i-like-to-do-with-git/
- An excellent book on Git.
- http://marklodato.github.io/visual-git-guide/index-en.html