Now, let's start using git!
Before we choose to use any Desktop client, let us learn how to interact with the command-line interface (CLI).
The git
command:
$ git
The git
command accepts options:
$ git --version
The git
command accepts sub-commands:
$ git log
Many git
sub-commands also accept options:
$ git log -p
If you are not sure how to use a sub-command, run:
$ git help add
$ git add --help
Many options have short and long "versions":
$ git commit -m "Fixed a typo."
$ git commit --message="Fixed a typo."
git init
A Git repository is a database containing all the information needed to retain and manage the revisions and history of a project.
A repository retains a complete copy of the entire project throughout its lifetime.
Let us create a repository.
$ mkdir ~/tmp/myrepo
$ cd ~/tmp/myrepo
$ echo 'My website is alive!' > index.html
To initialize a Git repository, run:
$ git init
git init
will create a hidden directory /.git
in the current directory. It does not matter if the directory
where you ran this command is empty or not.
To see the structure of the created .git
directory, run:
$ find .
or
$ tree .git
To install the tree
command, you may use:
homebrew
on macOSapt-get
on Linuxgit status
To check the current status of the repository, run:
$ git status
This command will show
git add
To stage a single file, run:
$ git add file.txt
To add multiple files, run:
$ git add file.txt file2.txt file3.txt
To add all files in a directory, run:
$ git add .
git commit
A commit is used to record changes to a repository.
Committing triggers git to record a snapshot of the sate of the repository.
Git does NOT store a copy of all the files in a repository, only those that changed since the last commit.
Commits are organized in a chain, with each commit pointing to one or more predecessors.
To commit staged changes, run:
$ git commit
This command will trigger the shell to open a default editor for you to describe your commit.
If you want to add your commit title, use the -m
option:
$ git commit -m "The title of my commit."
To also add a commit description, use a second -m
:
$ git commit -m "The title of my commit." -m "The description of my commit."
The normal process you will go through is:
# You create, edit or delete files
$ echo 'Hello world' > file.txt
# You stage your changes
$ git add file.txt
# You commit your changes
$ git commit -m "This message describes the commit."
You can stage and commit changes all at once by running:
$ git commit -a -m "Stage and commit all changes."
The option -a
will automatically commit all changes made to
tracked files, including new, modified or deleted files.
Here is an interaction in which we use all of the aforementioned commands.
$ mkdir ~/repos/file_state_repo
$ cd ~/repos/file_state_repo
$ git init
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
$ echo "New data" > file.txt
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
file.txt
nothing added to commit but untracked files present (use "git add" to track)
Create an example file
$ touch data.csv
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
file.txt
data.csv
$ git add data.csv
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: data.csv
Untracked files:
(use "git add <file>..." to include in what will be committed)
file.txt
$ git commit -m "Initial commit"
[master (root-commit) 1bf2af7] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 .gitignore
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
file.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git commit -m "Initial commit" --author "John <john.doe@unibz.it>"
$ git config user.name "John Doe"
$ git config user.email "john.doe@unibz.it"
$ git config --global user.name "John Doe"
$ git config --global user.email "john.doe@unibz.it"
To see all you current configuration, run:
$ git config --list
Sometimes, we will make a commit and realize that:
In such occasions, you can run
$ git add unstagedFile.txt
$ git commit --amend
If you simply want to change the commit title, run:
$ git commit --amend -m "My better title"
git rm
To remove a single file, run:
$ git rm file.txt
To remove multiple files, run:
$ git rm file.txt file2.txt file3.txt
You can also run:
$ rm file.txt
$ rm file2.txt
$ rm file3.text
$ git add .
To remove a file and keep it as unstaged, do
git rm --cached file.txt
git mv
To move or rename a single file, run:
$ git mv file.txt newFile.txt
or
$ mv file.txt newFile.txt
$ git rm file.txt
$ git add newFile.txt
or
$ mv file.txt newFile.txt
$ git add .
git reset
To unstage a change to a file, run:
git reset nope.txt
To unstage all staged files, run:
git reset
When running the last two commands, your changes will not be lost.
git reset
To undo the last commit and keep its changes unstaged, run:
$ git reset --mixed HEAD~1
To undo the last commit and keep its changes staged, run:
$ git reset --soft HEAD~1
To undo the last commit and throw away the changes, run:
$ git reset --hard HEAD~1
Note that:
HEAD
refers to the latest commit on the active branchHEAD~1
refers to the second last commit on the active
branchgit revert
Reverting doesn’t alter the existing history within a repository. Instead, it adds a new commit to its history.
To revert your last commit, run:
$ git revert HEAD
To revert any commit, run:
$ git revert 64c852bcb306bceeeec8f77708171c583d807408
To retrieve the commit history, run:
$ git log
To include the changes introduced in each commit, run:
$ git log -p
For more details on the last commit, run:
$ git show
For more details on a particular commit, run:
$ git show d6e5980bb9390e853e29b293bc8ecf024b237260
For a summary of the commits, run:
$ git show-branch --more=10w
help
in the command box to see a list of supported
operations
pres()
= Turn on presenter modeundo
= Undo the last git commandredo
= Redo the last undone git commandmode
= Change mode (local
or remote
)clear
= Clear the history pane and reset the visualization
Supported Git commands:
git branch
git checkout
git cherry_pick
git commit
git fetch
git log
git merge
git pull
git push
git rebase
git reflog
git reset
git rev_parse
git revert
git tag