Commit

Commit


Published on February 08, 2024.

Git Advanced / Part 1

git config

Change your local or global setup. Configure your name and e-mail

git config --global user.name <NAME>

Pasted image 20240118231216.png

git config --global user.email <Email>

Pasted image 20240118231230.png

git config --list

Pasted image 20240118231356.png

Important

If you are using GitHub is recommended to set your local Git config name and e-mail same as uset at GitHub account.

git status

Show the state of the current working directory and the staging area.

git status
Try it! Step 14

Pasted image 20240119195228.png

There are no commits yet in our project, but there is change to be committed. These changes are allocated in the staged area (index)

git commit

it allows storing in the database different versions of our project (snapshots)

Commits have similar structure as blob or tree

Git Advanced - part 1 2024-01-18 22.54.32.excalidraw.png

Try it! Step 14

Let´s try our first commit

git commit -m "Our first commit!"
Output

Pasted image 20240119182746.png

After commit a new object commit file was created (hash e5bbf15)

Pasted image 20240119183125.png

We can see the type of the file with the command git cat-file

Pasted image 20240119183329.png

The size

Pasted image 20240119183342.png

And finally, the content

Pasted image 20240119183400.png

Git Advanced - part 2 2024-01-19 18.35.41.excalidraw.png

Attention

The staging area (index) does not change after commit

And the status message says there's nothing to commit.

Pasted image 20240119194829.png

git log

Enables to browse previous change to a project

Try it! Step 15

if you type git log, you can see the change history

As you would expect, there´s only a single commit

git log 

Pasted image 20240119200343.png

if you want a pretty output in console, try:

git log --graph --decorate --pretty=oneline --abbrev-commit

To create an alias called tree is very easy

git config

git config <level> alias.<alias name> '<your sequence of git commands>'
Possible scopes:
--global level are available for all the repos for the current user
--system level are available for all the users/repositories
Default is repository level configs are only available for the current repo

git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit'

git tree

Pasted image 20240122000303.png

git blame

The blame command is a Git feature, designed to help you determine who made changes to a file line by line. I is used to show what revision and author last modified each line of a file. It's like checking the history of the development of a file.

git blame file1.txt
git blame --after=2023-01-01 file1.txt
git blame --since=3.weeks file1.txt
git blame --before="2 weeks ago" file1.txt

git add

Moves changes from the working directory to the staging area.

Let´s create another file

Try it! Step 16
echo "This is a new file" > file2.txt

if you verify the current state of the repository now

Pasted image 20240119201427.png

The new file will be warning as a untracked file.

There are usually 4-file tracking statuses possible

Git Advanced - part 2 2024-01-19 20.16.08.excalidraw.png

These tracking statuses have generally the following lifecycle

Git Advanced - part 2 2024-01-19 23.36.52.excalidraw.png

Git Advanced - part 2 2024-01-20 19.14.07.excalidraw.png

Try it! Step 17

The files will be moved to the stage area

 git add file2.txt

If you commit accidentally, you can untrack a file committed by mistake you can git rm --cache

Try it! Step 18
git rm --cache file2.txt

If you list the files on the staging area, it will not be there any more

git ls-files -s

Pasted image 20240120010916.png

The file file2.txt was removed from the staging area, but it remains on the working directory

ls -la

We return to the situation before git add

git status

Pasted image 20240120004323.png

So we add it again

Try it! Step 19
git add file2.txt

Pasted image 20240120011242.png

Git Advanced - part 2 2024-01-20 01.48.59.excalidraw.png

And the file will return to the staging area

git ls-files -s

Pasted image 20240120011316.png

git status

Pasted image 20240120011400.png

Try it! Step 20

After commit, 2 objects will be created (commit object file and a tree object file). A commit is just a wrapper for the tree object file.

git commit -m "Our second commit!"

Pasted image 20240120011633.png

Git Advanced - part 2 2024-01-20 01.51.59.excalidraw.png

Got to next part

Summary

# step 14
git status
# step 15 - Create a commit and a tree object
git commit -m "Our first commit!"
# Create a new file
echo "This is a new file" > file2.txt
# Add file2.txt to the stash (index)
git add file2.txt
# Remove file2.txt from the index
git rm --cache file2.txt
# Add file2.txt to the index
git add file2.txt
# Create a commit and a tree object
git commit -m "Our second commit!"

Commit