... it is actually quite clever!
Christian Olsen
https://chrede88.github.io/git-presentation/
December 21st, 2023
% brew install git # macOS
% apt install git # Ubuntu
% git config --global user.name "JohnDoe"
% git config --global user.email "JohnDoe@unknown.com"
% git config --global core.editor "code --wait"
% git config --global init.defaultBranch main # Github
% git config --global core.autocrlf true # Windows
% git config --global core.editor "atom --wait"
% git config --global core.editor "'c:/program files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
% git config --global core.editor "emacs"
% git config --global core.editor "vim"
% git config --global core.editor "gedit --wait --new-window"
% git config --global core.editor "open -W -n" # TextEdit on mac
Github allows you to hide your email and will provide you with an alternate email.
Find this setting under "Settings/Emails".
Something like: 0123456+username@users.noreply.github.com
% git config --global user.email "secret@email.com"
% git config --global --list
user.name=JohnDoe
user.email=JohnDoe@unknown.com
core.editor=code --wait
Using a secondary remote git service?
% git config user.email "SomeOther@email.com" # for each repo
% mkdir beer # create folder
% cd beer # move into folder
% git init # init a repository
% ls -a # list all files/folders
. .. .git
% code volta_brau.txt
% git status
% git add volta_brau.txt # adds one file at a time
% git add . # use . to add all changes
% git commit -m "Adding volta_brau.txt"
On branch main
nothing to commit, working tree clean
Add another line in the document and go through the steps to commit the changes to the repository.
% git status # check the status
% git add . # add the changes to the staging area
% git status # check the status again
% git commit -m "Adding another line of text"
% git log # prints the log
% git log -n 2 # prints the last 2 commit logs
commit 0123456789abcdef0123456789abcdef01234567
Author: JohnDoe JohnDoe@unknown.com
Date: Thu Jan 1 00:00:00 1970 -0000
Adding volta_brau.txt
% git checkout HEAD~1 # going back one step
% git checkout 0123456 # using the SHA1 hash
% git checkout - # only works for a fully detached HEAD
% git checkout main # always works
% git diff HEAD~1 volta_brau.txt
% git difftool HEAD~1 volta_brau.txt # opens in a diff tool
% git revert HEAD # or some older commit
Branches is mostly used when more than one person is collaborating in a repository. All past commits are carried over when a new branch is created.
Our three past commits along the main branch. And a new branch in magenta.
% git branch more_beer # create new branch
% git checkout more_beer # move to new branch
% git checkout -b more_beer # doing both commands in one go
% code volta_brau.txt # open and add more text
% code bbb.txt # create new file
% git add . # add everyting to the staging area
% git commit -em "Adding new file bbb.txt"
% git checkout main # move back to main
% git merge more_beer
% git branch -d more_beer # -d only remove if fully merged
% code README.md # Markdown format
% # beer <-- use hashtag for headline
% Some description of the repo
% git add .
% git commit -m "Adding a README"
% git remote add origin https://github.com/<username>/beer.git
% git remote add origin git@github.com:<username>/beer.git
% git remote -v # check that it worked
origin https://github.com/<username>/beer.git (fetch)
origin https://github.com/<username>/beer.git (push)
% git push origin main
% git clone https://github.com/<username>/gitr.git
% git clone git@github.com:<username>/gitr.git
In case you forgot 😉
% git checkout -b <newbranch> # make new branch
# make your changes
% git add . # add to staging area
% git commit -m "Made some changes" # commit to local repo
% git push origin <newbranch> # push to remote repo
It's a good idea to add a second "git remote" when contributing to other peoples repositories. Add a remote called upstream to the original repository. This way you can easily pull recent changes to your own repository.
% git remote add upstream https://github.com/<original-user>/somerepo.git
% git pull upstream main # pull upstream changes to original repo
Here is some text about Mars.
Mars is red.
<<<<<<< HEAD
I think Elon Musk will land there.
=======
Elon Musk will never land there.
>>>>>>> dabb4c8c450e8475aee9b14b4383acc99f42af1d
Here is some text about Mars.
Mars is red.
Elon Musk will never land there.