Git in 6 minutes.
Git is popular version control system. Every developer should know it, hence I made this simple small tutorial on how to use it from the command line. Basic shell knowledge is not really required but still helpful.
Installation.
Installation of git is out of the scope of this tutorial.
How to use Git?
Run:
man giteveryday
Now sit and read. :)
Now for the real tutorial.
What's Git?
Git is a decentralized version control system. Decentralized means that there is no central server to which changes are made and kept. Any git instance is a full 'repository' on its own.
However many people use hosting platforms like GitHub, which people treat as the central server.
A git 'repository' is a directory in which there is a .git/ directory,
and your project files. The .git/ folder has all the information that git
needs.
Untracked/Tracked/Staged files.
Untracked files are not present in the git history. When sharing your repository, the other person will not receive these files.
Tracked files' history are tracked by git.
Staged files are those files which will be commited next the next time
git commit is run. This simply means that git will log the change in its
database.
HEAD.
HEAD refers to the most recent commit.
Quickstart git commands.
Tell git about yourself.
When you first install git, it will not know who you are. You need to tell it who you are so that it can sign commits with your name. To tell it that, use:
git config --global user.name "your-name"
git config --global user.email "your-email"
Also most modern platform default to the name main as the default branch, so
better configure that now as well:
git config --global init.defaultBranch main
git init.
This command simply initializes the current directory as a git repository.
git add.
This command changes untracked files to tracked files and stages changes made
in those files. The staged files will be commited when running git commit.
To stage the entire current directory for tracking run:
git add .
git commit.
This command is used to 'commit' the changes to tracked files. This causes
git to log an entry in its database with the date and a message which
describes your changes.
Simply running git commit causes your default editor (configured by the
environment variable $EDITOR) to open which prompts you to type in the
commit message. The commit message can otherwise be passed into the command
directly like so:
git commit -m "commit message goes here"
To stage changes in all files that have changed and commit them with a message directly, use:
git commit -am "commit message"
git reset.
This command resets the HEAD to a known state. There are two primary
options when using reset to go to a known state (a known state is just a
commit). Commits are referred by their hashes (the first 6 or 8 digits of the
hash is enough for git to figure out which commit you are trying to refer to).
To reset HEAD to the previous commit, removing all files and overwriting any
to exactly match the commit:
git reset --hard "commit-hash"
To not overwrite or delete anything run:
git reset --soft "commit-hash"
git restore.
This command is useful if you accidentally delete something from the
repository. Suppose you accidentally removed a tracked file <file>, to
restore it, run:
git restore "filename"
To restore all files in the current directory:
git restore .
git revert.
Suppose you have an existing commit that you want to remove. git revert does
exactly that.
To revert the last commit:
git revert HEAD
This will create a new commit which reverts the changes made in the last commit.
To revert the 2nd last commit:
git revert HEAD~1
The tilde followed by a number (n) after HEAD represents the nth previous
commit from HEAD.
Commands to work with remote hosts (like GitHub).
Typically git repositories are hosted remotely, mostly on Github because it is super popular. This section quickly teaches you the three basic commands to work with remote hosts like GitHub.
git remote.
Used to add/update/remove a remote host. You can have multiple remote hosts but for 95% of the cases, this command should suffice:
git remote add origin "url-here"
Make sure the <url> actually points to an empty git repository which you
have push access to. Also origin is just the standard name; it can be
anything.
To add another remote called upstream for example, you would use:
git remote add upstream "another-url"
git push.
For the first time you push your changes to the remote, you would need to tell it which remote do you want to push to.
For our example, it would simply be:
git push -u origin main
Subsequent pushes are simply just:
git push
git pull.
To pull changes from remote (if remote changed) use:
git pull --rebase
More...
That should do it for Git in 6 minutes. If you want to know more about Git, then use the manual pages. Reading is indeed a very good habit :)