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 statusToday we will cover
push 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!my-project local repositorygit logpush our changes to a remote repository on 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 mergetooladd 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 mergetoolmain on GitHub, and master in older repositoriesmaster 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 master branch
git checkout mastergit merge feature-1git pushDone! Let’s see what happened
You can delete the branch with git branch -d feature-1
To delete the branch on GitHub, use git push origin --delete 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 and throws away every commit after it
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 pushgit clone https://github.com/username/repository-name.git
git clone https://github.com/danilofreire/my-project.gitFork button on the top right cornerorigin is your fork. upstream is the repository you forked fromIssues tab on GitHub and then on the New issue buttonPull requests tab on GitHub and then on the New pull request button+ sign on the top right corner of GitHub and then on New gistyour-username.github.io.html, .css, and .js files to the repositoryhttps://your-username.github.io.yml file listing the steps. GitHub runs them whenever you pushbrew install ghgh repo create my-projectgh repo clone danilofreire/my-projectgit 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 commitgit 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 🤓