Getting started with Git
Table of Contents
- 1. why use a version control system (VCS)?
- 2. interacting with Git
- 3. creating or cloning a repository
- 4. history and commits
- 4.1.
git log
shows the history of changes to the repository - 4.2. the history is a list of commits
- 4.3. a repository’s history is local, complete, and immutable
- 4.4. each commit has a unique 20-byte identifier (SHA1 hash), the commit ID
- 4.5.
git show COMMIT-ID
shows the changes introduced by the given commit - 4.6.
git diff A..B
shows the changes between commits A and B
- 4.1.
- 5. references
- 6. branches
- 7. working directory vs. index vs. history
- 7.1. the files you edit are in the working directory
- 7.2.
git add
adds changes to the index, which is a staging area - 7.3.
git commit
commits what the index contains to the history - 7.4.
git status
reports on changes in the WD and in the index - 7.5.
git diff HEAD
shows changes compared to the tip of the current branch
- 8. discarding or stashing away local changes
- 9. remotes, push, pull
1 why use a version control system (VCS)?
1.1 doing it manually…
1.2 the other options: Subversion, Mercurial, etc.
1.3 Git vs. GitHub vs. GitLab vs. gitlab.com vs. gitlab.inria.fr
1.3.1 these things are “just” hosting services!
2 interacting with Git
2.1 at the command line with git
, sometimes inconvenient
2.2 Magit (Emacs)
2.3 Fugitive (Vim)
2.4 gitk (read-only), comes with Git
2.5 web interface at gitlab.inria.fr, etc.
3 creating or cloning a repository
3.1 for a brand new repo
git init
git add . # Add all the files under the current directory.
git commit -m "This is the first commit."
3.2 for an existing repo
git clone https://gitlab.inria.fr/guix-hpc/guix-hpc.git
cd guix-hpc
What you obtain is a checkout.
4 history and commits
4.1 git log
shows the history of changes to the repository
4.2 the history is a list of commits
4.3 a repository’s history is local, complete, and immutable
4.4 each commit has a unique 20-byte identifier (SHA1 hash), the commit ID
The commit ID can be shortened (e.g., take the seven first hexadecimal digits), as long as it’s unambiguous.
If you “amend” a commit, you get a different commit ID, so effectively a different commit and a different history.
Each commit has an author: name + email address.t0
4.5 git show COMMIT-ID
shows the changes introduced by the given commit
4.6 git diff A..B
shows the changes between commits A and B
5 references
5.1 HEAD
is the tip of the current branch
5.2 foo
is the tip of branch foo
5.3 master^
is the next-to-last commit of branch master
5.4 master^^^
is… guess what…
5.5 HEAD^^^
, likewise but for the current branch
6 branches
6.1 branches represent different lines of history, which can be merged
Use git branch -l
to list the local branches.
6.2 the “default” branch is called master
6.3 git branch NAME
creates new branch NAME off the current branch
6.4 use git checkout BRANCH
to switch the working tree to an existing branch
7 working directory vs. index vs. history
7.1 the files you edit are in the working directory
7.2 git add
adds changes to the index, which is a staging area
7.3 git commit
commits what the index contains to the history
7.4 git status
reports on changes in the WD and in the index
7.5 git diff HEAD
shows changes compared to the tip of the current branch
8 discarding or stashing away local changes
8.1 git reset --hard
to discard uncommitted changes
8.2 git stash
to stash away uncommitted changes
Run git stash pop
to reinstate those changes in the working tree,
uncommitted.
9 remotes, push, pull
9.1 repositories can have associated remotes
9.1.1 a remote is a remote copy (clone) of the repository
9.1.2 a repository can have several remotes
$ git remote -v
gbor https://github.com/Boskovits/guix (fetch)
gbor https://github.com/Boskovits/guix (push)
janneke https://gitlab.com/janneke/guix (fetch)
janneke https://gitlab.com/janneke/guix (push)
origin ssh://civodul@git.sv.gnu.org/srv/git/guix.git (fetch)
origin ssh://civodul@git.sv.gnu.org/srv/git/guix.git (push)