git config --global user.name "<Your Name>"
git config --global user.email "<your@email>"
git config --global user.name "John Doe"
git config --global user.email "john.doe@gmail.com"
git config --global core.editor "<text-editor>"
git config --global core.editor "nano"
git config --global core.autocrlf input
git config --global core.autocrlf true
git config --list
git <command> --help
git help <command>
man git-<command>
git commit --help
git help commit
man git-commit
git <command> --help
git help <command>
man git-<command>
SPACE scroll one screen down
b scroll one screen up
q quit
git <command> -h
git commit -h
Git saves the history of a project as a series of snapshots
Those snapshots are called commits
Commits are identified by unique hash
Each commit contains these metadata: - author - date and time - the hash of parent commit(s) - a message
As soon as you create the first commit, a pointer called a branch is created and it points to that commit. By default, that first branch is called main
Another pointed (HEAD
) points to the branch main
HEAD
indicates where we are in the project history
As you create more commits, the history grows …
… and the pointers HEAD
and main
automatically move to the last commit
For simplicity, the diagrams can be simplified this way
git log --oneline
git log \
--graph \
--date-order \
--date=short \
--pretty=format:'%C(cyan)%h %C(blue)%ar %C(auto)%d'`
`'%C(yellow)%s%+b %C(magenta)%ae'
git log --graph
git log --graph --all
A useful representation of Git’s functioning is to imagine three file trees
When you work on your project, your working tree changes
You organize your next snapshot by picking and choosing some changes
git add <what-you-want-to-commit-next>
Those changes move to the index or staging area
Finally you create a commit with what is in the staging area
git commit -m "<message>"
Finally you create a commit with what is in the staging area
Copies of a project & its history
Anywhere, including on external drive or on the same machine as the project
Often on a different machine to serve as backup or on a network (e.g. internet) to serve as syncing hub for collaborations
Popular online Git repository managers & hosting services:
You create a project on your machine & want others to contribute to it (1)
You want to contribute to a project started by others & …
… you have write access to it (2)
… you do not have write access to it (3)
Go to https://github.com , login, & go to your home page
Look for the Repositories tab & click the green New button
Enter the name you want for your repo, without spaces
Make the repository public or private
Click on the Code green drop-down button, select SSH if you have set SSH for your GitHub account or HTTPS & copy the address
In command line, cd
inside your project & add the remote
git remote add <remote-name> <remote-address>
<remote-name>
is a convenience name to identify that remote. You can choose any name, but since Git automatically call the remote origin
when you clone a repo, it is common practice to use origin
as the name for the first remote
git remote add origin git@github.com:<user>/<repo>.git
git remote add origin https://github.com/<user>/<repo>.git
If you are working alone on this project & only wanted to have a remote for backup, you are set
If you don’t want to grant others write access to the project & only accept contributions through pull requests, you are also set
If you want to grant your collaborators write access to the project however, you need to add them to it
cd
to location where you want your local copy, then
git clone <remote-address> <local-name>
This sets the project as a remote to your new local copy & that remote is automatically called origin
Without <local-name>
, the repo will have the name of the last part of the remote address
Fork the project
Clone your fork on your machine
Add the initial project as a second remote & call it upstream
List remotes:
git remote
List remotes with their addresses:
git remote -v
Get more information on a remote:
git remote show <remote-name>
git remote show origin
Rename a remote:
git remote rename <old-remote-name> <new-remote-name>
Delete a remote:
git remote remove <remote-name>
Change the address of a remote:
git remote set-url <remote-name> <new-url> [<old-url>]
If you collaborate on a project, you have to get the data added by your teammates to keep your local project up to date
To download new data from a remote, you have 2 options:
git fetch
git pull
Fetching downloads the data from a remote that you don’t already have in your local version of the project
git fetch <remote-name>
The branches on the remote are now accessible locally as <remote-name>/<branch>
. You can inspect them or you can merge them into your local branches
git fetch origin
Pulling fetches the changes & merges them onto your local branches
git pull <remote-name> <branch>
git pull origin main
If your branch is already tracking a remote branch, you can omit the arguments
git pull
Uploading data to the remote is called pushing
git push <remote-name> <branch-name>
git push origin main
You can set an upstream branch to track a local branch with the -u
flag
git push -u <remote-name> <branch-name>
git push -u origin main
From now on, all you have to run when you are on main
is:
git push
upstream
to update your local project
origin
— remember that you do not have write access to upstream
)