1. Common aliases
For Windows users:
git config --global alias.co checkout git config --global alias.ci commit git config --global alias.st status git config --global alias.br branch git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short" git config --global alias.type 'cat-file -t' git config --global alias.dump 'cat-file -p'
Also, for users of Unix/Mac:
, , , and are common commands so it is a good idea to have abbreviations for them.
Add the following to the
.gitconfig file in your
[alias] co = checkout ci = commit st = status br = branch hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short type = cat-file -t dump = cat-file -p
We’ve already talked about commit and status commands. In the previous part we covered the
log command and will get to know the checkout command very soon. The most important thing to learn from this part is that you can type
git st wherever you had to type
git status. Best of all, the
git hist command will help you avoid the really long
- Define the
histalias in the .gitconfig file
For the most part, I will continue to type out the full command in these instructions. The only exception is that I will use the
hist alias defined above, when I need to see the git log. Make sure you have a
hist alias setup in your
.gitconfig file before continuing if you wish to repeat my actions.
We’ve added a few aliases for commands we haven’t yet discussed. We will talk about the
git branch command very soon, and the
git cat-file command is useful for exploring git.
- Command aliases (optional)
alias gs='git status ' alias ga='git add ' alias gb='git branch ' alias gc='git commit' alias gd='git diff' alias gco='git checkout ' alias gk='gitk --all&' alias gx='gitx --all'
gco abbreviation for
git checkout is very useful, allowing me to type:
to checkout a particular branch.
2. Getting older versions
- Getting hashes for the previous versions
You will see:
* aa1c121 2021-03-09 | Added HTML header (HEAD, master) [author name] * bb1c121 2021-03-09 | Added standard HTML page tags [author name] * cc1c121 2021-03-09 | Added h1 tag [author name] * dd1c121 2021-03-09 | First Commit [author name]
Check the log data and find the hash for the first commit. You will find it in the last line of the
git hist data. Use the code (its first 7 chars are enough) in the command below. After that check the contents of the hello.html file.
Note: Many commands depend on the hash values in the repository. Since my hash values will be different from yours, substitute in the appropriate hash value for your repository everytime you see
<treehash> in the command.
$ git checkout dd1c121 Note: checking out 'dd1c121'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at dd1c121... First Commit $ cat hello.html Hello, World!
checkout command output totally clarifies the situation. Older git versions will complain about not being on a local branch. But you don’t need to worry about that right now.
Note that the content of the
hello.html file is the default content.
- Returning to the latest version in the master branch
// git checkout <branch> git checkout master cat hello.html
$ git checkout master Previous HEAD position was 911e8c9... First Commit Switched to branch 'master' $ cat hello.html <html> <head> </head> <body> <h1>Hello, World!</h1> </body> </html>
‘master’ is the name of the default branch. By checking out a branch by name, you go to its latest version.
3. Tagging versions
- Creating a tag for the first version
git tag v2
Now, the current version of the page is referred to as v2.
- Tags for previous versions
Let’s tag the version prior to the current version with the name v1-beta. First of all we will checkout the previous version. Instead of looking up the hash, we are going to use the
^ notation indicating “the parent of v2”.
v2^ notation causes troubles, try using
v2~1, referencing the same version. This notation means “the first version prior to v2”.
git checkout v2^ cat hello.html
git tag v1-beta
git checkout v2 git checkout v1-beta
A list of available tags:
$ git tag --- v1 --- v1-beta
- Viewing tags in logs
$ git hist master --all * wa3c151 2021-04-09 | Added HTML header (v2, master) [author name] * 1c32387 2021-04-09 | Added standard HTML page tags (HEAD, v1-beta) [author name] * 53625f7 2021-04-09 | Added h1 tag [author name] * 111e8w9 2021-04-09 | First Commit [author name]
4. Discarding local changes (before staging)
- Make sure you are on the lastest commit in the master brach before you continue.
git checkout master
- Change hello.html
<html> <head> </head> <body> <h1>Hello, World!</h1> <!-- This is a bad comment. We want to revert it. --> </body> </html>
- Check the status
We see that the
hello.html file has been modified, but not staged yet.
- Undoing the changes in the working directory
git checkout hello.html git status cat hello.html
The status command shows there were no unstaged changes in the working directory. And the “bad comment” is no longer contained in the file.
5. Cancel Staged changes (before committing)
- Edit file and stage changes
Make changes to the
hello.html file in the form of an unwanted comment
<html> <head> <!-- This is an unwanted but staged comment --> </head> <body> <h1>Hello, World!</h1> </body> </html>
Stage the modified file.
git add hello.html
- Check the status of unwanted changes.
- Reset the buffer zone
git reset HEAD hello.html
reset command (default) does not change the working directory. Therefore, the working directory still contains unwanted comments. We can use the checkout command from the previous tutorial to remove unwanted changes from working directory.
reset command resets the buffer zone to HEAD. This clears the buffer zone from the changes that we have just staged.
git checkout hello.html git status