Lecture 06 - More Git and GitHub
brew install git)git config --global user.name "Your Name"git config --global user.email your@email.cominit(ialising) repositoriesadd and commit changesgit loggit checkout to go back in timegit statusSource: xkcd.com
Today we will cover
gh), and using two GitHub accountspush and pull changes to and from remote repositories.gitignore to avoid tracking certain filesclone and fork repositories on GitHubissues and pull requests on GitHubmaster to mainmaster instead of mainmaster to main to use more inclusive languagemaster to main:-m flag stands for “move” (rename)mainmaster in your workflows!brew install ghgh repo create my-project: new repositorygh repo clone danilofreire/my-project: copy to your computergh repo fork torvalds/linux: copy to your GitHub account--global sets defaults for all repositories; omit it to set values for one repository onlygh stores several accounts and switches between themgh auth login: log in to an accountgh auth status: check which account is activegh auth switch: change the active accountgh auth logout: log out of the current accountmy-project local repositorygit logpush our changes to GitHub+ sign on the top right cornerNew repositorymy-projectCreate repositorypush our changes!git remote add origin https://github.com/danilofreire/my-project.git:
remote: tells Git we are adding a remote repositoryadd: adds a new remote repositoryorigin: the name of the remote repositoryhttps://github.com/danilofreire/my-project.git is the URL of the remote repositorygit branch -M main: makes sure the branch is called maingit push -u origin main: pushes the changes, and remembers where to push next timepush further changes to the remote repository, you just repeat the process:
git add file-namegit commit -m "Message"git push (no need to add the remote repository again)pull changes from GitHubpull changes from a remote repository, use (you’ve guessed it) the command git pullgit pull is the equivalent of git fetch + git merge
git fetch downloads the changes from the remote repositorygit merge merges the changes with your local repository.gitignore file to ignore certain files and directories.gitignore to our repositoryAdd file > Create new file, then commit.gitignore lists what Git should never track:
secrets.txt*.csv for every CSVdata/pull 😉git mergetoolpull.rebase false – merges: keeps both histories and adds a merge commit. Safest choicepull.rebase true – rebases: replays your commits on top of GitHub’s for a linear history. Avoid it on commits you already pushedpull.ff only – fast-forwards only: you decide what to doadd the file and commit the changespush the changes to GitHubGit marks the conflict with three lines:
<<<<<<< opens your version======= separates the two versions>>>>>>> closes their version, and names the commit it came fromDelete the markers along with the text you do not want
git mergetool (here for more information)main on GitHub, and master in older repositories and in Git’s own defaultmain branchgit branch branch-namegit checkout branch-name
git switch branch-name if you have Git 2.23 or latergit checkout -b branch-namefeature-1
git checkout -b feature-1feature-1.txt to the new branch
echo "This is feature 1" > feature-1.txtadd, commit, and push the changes to GitHub
git add feature-1.txt && git commit -m "Add feature 1"git push origin feature-1 (you need to push the new branch to GitHub)Now the branch is already on GitHub
Let’s merge the changes to the main branch
git checkout main (or master)git merge feature-1git pushDone! Let’s see what happened
You can delete the branch with this command: git branch -d feature-1
To delete the branch on GitHub, use this one: git push origin -d feature-1
More about branches here
git log --oneline lists the short hashesThree ways back, from safest to most destructive:
Look around: git checkout <hash>. You are now in detached HEAD, so anything you commit belongs to no branch
Keep your work: git switch -c new-branch <hash> starts a branch at that commit. Usually what you want
Move the branch itself: git reset --hard <hash> rewinds your branch, drops every later commit, and deletes uncommitted changes for good
git switch main brings you back to the present
0-9 and a-f onlya1b2c3d is enough. The full form is 40 characters, like 470636f38e409f4f322c48183e19633ebb550625git refloggit switch -c and git checkout -b do the same thing. switch is the newer, clearer name (Git 2.23+)git branch -d branch-name deletes a branch you have finished with-D forces the deletion, even when the work was never mergedmain altogether. Delete it, rename your current branch to main, then git push --forcegit clone https://github.com/username/repository-name.git
git clone https://github.com/danilofreire/my-project.gitgit fork command existsgh repo fork owner/repo, as we saw earlierFork button on the top right cornergh:origin is your fork. upstream is the repository you forked fromIssues tab on GitHub and then on the New issue buttongh: gh issue create --title "Plot is empty" --body "Details"Pull requests tab on GitHub and then on the New pull request buttongh pr create --fill (title and body come from your commits)+ sign on the top right corner of GitHub and then on New gistgh gist create notes.py (secret by default; add --public to list it)your-username.github.io.html, .css, and .js files to the repositoryhttps://your-username.github.ioMore information here: https://pages.github.com/
.yml file listing the steps. GitHub runs them whenever you pushRead more: https://github.com/features/actions
git push sends your commits to GitHub, git pull brings other people’s back.gitignore keeps data, keys, and generated output out of the repositorymain, and merge brings the work backgit switch -c new-branch <hash> is the safe way to revisit an old commitgh brings repositories, issues, pull requests, and Gists to your terminalgit diff: see exactly what changed, line by line, before you commitgit resetgit cherry-pick: take one commit from a branch without merging the whole thinggit rebase: replay your commits on top of someone else’s workmy-project repository. We will keep working on it 🤓