Git Tutorial Notes

Track history and work together. Distributed version control system.

Why command line:

  1. All commands are supported
  2. Command line is always* available

Git Config#

git config --help
git config --global user.name "David Nevin"
git config --global user.email david@......com
git config --global core.autocrlf input (for macOS of Linux)

Git Workflow#

git add <file>
git commit (-m"short message)

Commit without adding first (not recommended)

git commit -a

Commit often and keep the commits small. Write meaningful commit messages.

If applied, this commit will your subject line here.

An “atomic” change revolves around one task or one fix.

Seven rules for a great commit. https://cbea.ms/git-commit/

The seven rules of a great Git commit message#

  1. Separate subject from body with a blank line
  2. Limit the subject line to 50 characters
  3. Capitalize the subject line
  4. Do not end the subject line with a period
  5. Use the imperative mood in the subject line
  6. Wrap the body at 72 characters
  7. Use the body to explain what and why vs. how

Delete File#

Deleted and commit the deletion directly.

git rm <file>

Rename File#

Rename and commit rename directly.

git mv <old_filename> <new_filename>

Staging Area#

List files in staging area.

git ls-files

Remove empty folder or file form staging area.

git rm --cached <filename or foldername> # don't forget the --cached or the file will be deleted!

Remove an entire directory from the staging area.

git rm --cached -r <foldername> # -r applies deletion recursively

Git Short Status#

Get a concise status. Only shows the files affected.

git status -s

Git diff#

See the changes on a line by line basis, can be difficult on the command line.

git diff --staged
diff --git a/hello.txt b/hello.txt    # a/old file b/new file
new file mode 100644                  # mode
index 0000000..94954ab
--- /dev/null                         # no old copy (new file mode)
+++ b/hello.txt                       # additions to b/newfile
@@ -0,0 +1,2 @@                       # - old file, (start,end of lines extracted), + new file 1,2 (start,end of lines extracted)
+hello                                # details the changes
+world

To use vim as the diff tool use the following command.

git config --global diff.tool vimdiff

Git History#

To see the history or logs of our commit use:

git log
git log --oneline
git log --oneline --reverse

Viewing Commits#

To see the change made in a particular commit use:

git log --oneline
git show HEAD
git show as009s # uning commit ref.
git show HEAD~3 # third before HEAD

Unstaging Files#

To remove changes from the staging area. We want to undo the add operation.

git restore --staged <filename>

Git Log#

To inspect the logs use:

git log --oneline --stat # show stats of oneline log
git log --stat           # show stats of full log
git log --online --patch # show the actual changes in each

Git Log Filtering#

git log --oneline -3 # last 3 commits
git log --oneline --author="Name"         # Commited authoured by Name
git log --oneline --after="YYYY-MM-DD"    # commits after the specified date
git log --oneline --before="YYYY-MM-DD"   # commits before the specified date
git log --oneline --after="One week ago / yesterday / today" # can use relative terms
git log --oneline --grep="GUI"            # search in commits for the phrase "GUI" (see sensitive)
git log --onleline -S"hello()"            # search the contents of the changes for hello()
git log --onleline -S"hello()" --patch    # search the contents of the changes for hello() with details of the changes
git log --oneline 91f7...36cd6            # using commit references from....to (no need for full commit ref, just enough not to be ambiguous)
git log --oneline <filename>              # related to a particular file
git log --online -- <filename>            # if the filename is ambiguous or causes confusion.
git log --online --patch -- <filename>    # for details of above command

Git Log Formatting#

git log --pretty=format:"%an committed %h on %cd"   # displays (authorname) committed (abbreviated hash) on (date)
git log --pretty=format:"%Cgreen%an%Creset committed %h on %cd"   # displays ChangeColorToGreen(authorname)ResetColor committed (abbreviated hash) on (date)"

Viewing a commit#

To see the files changed in a particular commit.

git show HEAD~2 --name-only                # shows the files changed in the second to last commit
git show HEAD~2 --name-status              # shows the files changed in the second to last commit
git show HEAD~2:full/path/to/file/filemane #shows the changes made to that file in the second to last commit

Viewing Changes Across Commits#

To view the commits across a number of distinct commits:

git diff HEAD~2 HEAD                       # changes between the second to last commit and the current head
git diff HEAD~2 HEAD <filename>            # changes between the second to last commit and the current head of <filename>
git diff HEAD~2 HEAD --name-only           # changes between the second to last commit and the current head listing the files
git diff HEAD~2 HEAD --name-status         # changes between the second to last commit and the current head of <filename> and status

Checkout an Earlier Commit#

To set the working directory to exactly how it was at that commit:

git checkout <commit reference> # Just to look around, not make changes
git checkout master             # to get back to the branch and exit the commit.

Finding Bugs using Git Bisect#

Know a good commit start point and the commit which we consider bad, ie. the bug is present. Git bisect is a manual binary search. At each point we run or tests and if the biscet is good or bad, i.e., the bug is not present or not, we define the biscet at this point to be good or bad. This continues until we narrow down the search to a single commit. We can then exit bisect.

git bisect start
git bisect bad # set the current location of the HEAD as the bad commit
git bisect good <good commit ref> # the start of our search
# continue to mark as good or bad each commit as presented, until there is only one left.
git bisect reset # to exit bisect and return to the commit you were on before starting biscet.

Finding Contributors using Shortlog#

git shortlog                # Shows commit messages sorted by Author
git shortlog -n             # Order by the number of commits
git shortlog -s             # supress summary, only shows author
git shortlog -e             # show the email address of each author
git shortlog --before="" --after="" # for a given data range

Viewing History of File#

git log (--online --all) <filename>
git log --stat <filename>
git log --patch <filename>

Restoring a Deleted File#

If we have accidentally deleted a file and committed the deletion, we can restore the file using:

git log --online -- <filename> # note extra -- and space before filename to avoid ambiguous command.
# we identify the parent commit, i.e. the commit before the deletion.
git checkout <commit parent ref> <path to file>
# Then commit the file.
git commit -m "Relevent message"

Finding the Author using Blame#

git blame <filename>    # show author for file
git blame -e <filename> # dislay author email
git blame -e -L 1,3     # limit output to the first three lines

Tagging#

To bookmark a commit, for example a version change, we can use tagging.

git tag v2.1.0 <commit ref, i.e. 5e78...>    # creates a lightweigh tag, if not commit refernce is added, the current commit is tagged.

Once added we can reference this commit using the tag, e.g. checkout.

git checkout v2.1.0

Then to list all tags use:

git tag

Annotate tag:

git tag v.2.1.1 <commit ref, i.e. 5e78...> -a -m "Meaningful message" # Using an annotated tag we can add a message to the tag.

To list all tags with messages:

git tag -n # lightweight tags will take the commit message as their message, whereas annotated tags will display the message applied to them

To delete a tag use:

git tag -d <tagname>

initial source:

https://www.youtube.com/watch?v=8JJ101D3knE