Notes from Code School's Git Real Course - Level 7
Viewing the Log
$ git log
Use git log to view a history of your recent commits. The log output will show the SHA (secure hash algorithem, pronounced 'shah'), author, date, and commit message.
![]() |
typical output after running $ git log |
Colorizing the Log
The log output isn't always the easiest to read. You can add colors to the log to help make it more readable. Run this to emphasize your commit hash every single time.
$ git config --global color.ui true
You can also customize the format the log output. The below command will give a shortened version of the log with one commit per line, showing the SHA hash and the commit message.
$ git log --pretty=oneline
![]() |
how to simplify your git log output |
There are many different ways to format your log output. You can use placeholders to format the output of the log for each commit, for example:
$ git log --pretty=format: "%h %ad- %s [%an]"
Placeholder: Replaced With:
%ad author date
%an author name
%h SHA hash
%s subject
%d ref names
run git help log for more options
Note: Up to this point the course has not covered log subjects or ref. names, hopefully it gets addressed in the more advanced Git course.
Patch
If you want to see what each commit changed, you can do:
$ git log --oneline -p
This will show which lines were removed and which lines were added for the files that were modified for each commit.
$ git log --oneline --stat
Shows how many insertions and deletions were made for each file included in each commit
Graph
$ git log --oneline --graph
Gives a visual representation of the branches and the commits on them
Date Ranges
Its not always useful to see the entire log. You can narrow down your results based on relative and absolute date ranges.
Until
$ git log --until=1.minute.ago
Since
$ git log --since=1.day.ago
$ git log --since=1.week.ago
$ git log --since=1.month.ago
Since and Until (Relative)
$ git log --since=1.month.ago --until=2.weeks.ago
Since and Until (Absolute)
$ git log --since=2000-01-01 --until=2012-12-21
Diffs
Git diff can be really useful to see what has changed since our last commit. It will show removed lines and added lines.
Uncommitted Changes
$ git diff HEAD
This will do the same thing as $ git diff, which is to show the difference between now and the most recent commit
Earlier Commits
To see the difference between now and even earlier commits, you can run:
$ git diff HEAD^ (parent of last commit)
$ git diff HEAD^^ (grandparent of last commit)
$ git diff HEAD~5 (five commits again)
How to Compare Two Commits:
$ git diff HEAD^..HEAD (compares second most recent and the most recent commit)
You can also use SHA hashes to compare commits:
$ git diff <SHA1>..<SHA2> (range of SHAs)
Abbreviated SHAs
To see abbreviated SHAs do:
$ git log --oneline
You can also grab the abbreviated SHAs from Github.com
$ git diff 4f6063f..f5a6ff9 (range of abbreviated SHAs)
Diff Branches
You can also use branches to run diffs:
$ git diff master bird
This would show the differences between the master branch and a branch called 'bird'
Time Based Diffs
Like git log, you can also use time based ranges to see diffs:
$ git diff --since=1.week.ago --until=1.minute.ago
Blame
When projects go on for a while, you may see changes made to the code and you can't remember or don't know who or when it was made. To see an output of all the changes in a file, as well as who made the change and the date it was changed, run:
$ git blame <filename>
e.g. $ git blame index.html
Excluding Files
There may be a time when you want to work on a folder, but don't want that folder included in the repository.
For example, say we have a folder called experiments/ that we don't want included in the repository.
You must type the name of the folder manually into a 'git exclude' file. The directory path would look like this .git/info/exclude. Open 'exclude' in a text editor and add experiments/ on the first line. Save close and commit. This will make that folder (and any other items listed) invisible to git.
Exclude Patterns
Other ways to exclude files are to add patterns to the exclude file. For example by typing the following items in your exclude file, you can exclude all that are like them.
tutorial.mp4
Exclude a specific file
*.mp4
Exclude all mp4 files
experiments/
Exclude entire experiments folder
logs/*.log
Exclude all log files located in the log directory
Excluding from All Copies
No one should ever include log files inside a repository. The log files change differently depending on whose using this application. These log files can cause conflicts and errors if included. Instead, you should use a .gitignore file, this will tell git to ignore not just your local log files, but everyones else's too.
For example, add this pattern to your .gitignore file
logs/*.log
Once you commit the above change, git will ignore all .log files
Deleting Files
$ git rm <file_name>
e.g. $ git rm README.txt
This step will delete the file from the local file system and untrack it.
Next do:
$ git commit -m "remove README"
and that file will disappear.
Untracking Files
If you want to untrack a file, but not delete it from your local file system. For example, you need to add your development log to your .gitignore file, but it is already being tracked. Do:
$ git rm --cached development.log
This will remove the development log from git tracking. You can now add logs/*.log to your .gitignore file, then do $ git add .gitignore, and $ git commit -m "ignore all log files" to make it all log files invisible to git.
To untrack any file do: $ git rm --cached <file_name>
Config
In LEVEL 1 Codeschool covered some basic configuration with user.name and user.email. You can also do some more advanced configuration.
For example, to specify the interactive editor of your choice do:
$ git config --global core.editor emacs
This will use emacs for all interactive commands. Or more useful for me, because I use Sublime Text 2 on a Mac (note this will be different for Windows users).
$ git config --core.editor "open -a 'Sublime Text 2'"
Also use:
$ git config --global merge.tool opendiff
User opendiff to see merge conflicts in a better way (OS X only)
Local Config
How to Set a Different Email for a Specific Repository
First, this is how you set the email for all your repositories
$ git config --global user.email "email@example.com"
Now, to set the email for the current repository:
$ git config user.email "special_email@example.com"
To see a list of your user setup run:
$ git config --list
This will show your global user email first, and your local user email second.
You can also do:
$ git config user.email
to see the local user email (if you want to make sure its set properly)
Aliases for Log Formats
You can create aliases for frequently used, often complicated, log formats. One popular formats:
$ git config --global alias.mylog "log --pretty=format:'%h %s [%an]' --graph"
$ git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit --all"
Then to run, just do:
$ git mylog or $ git lol
To create aliases for commonly used commands do:
$ git config --global alias.st status
This will map st --> status. Now you can run $ git st instead of $ git status
e.g.s
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
To create any alias do:
$ git config --global alias.<name_of_alias> <command>
No comments:
Post a Comment