The document provides an overview of version control and the Git version control system. It covers basic Git commands like init, add, commit, status, log and branching. It also discusses ignoring files, stashing changes, and using GitHub for collaboration.
The document provides an overview of version control and the Git version control system. It covers basic Git commands like init, add, commit, status, log and branching. It also discusses ignoring files, stashing changes, and using GitHub for collaboration.
Ignore specific files Git logs all the commits you've made. Checking the log is helpful for understanding the history of your project.
Cheat Sheet - Git
.DS_Store secret_key.txt Check log in default format Ignore files with specific extensions $ git log commit dc2ebd6... (HEAD -> main) *.pyc Author: Eric Matthes <[email protected]> Version Control Date: Feb 27 11:27:07 2023 -0900 Version control software allows you to take snapshots Initializing a repository Greets user. of a project whenever it’s in a working state. If your All the files Git uses to manage the repository are located in commit bf55851... project stops working, you can roll back to the most ... the hidden directory .git. Don't delete that directory, or you'll recent working version of the project. lose your project's history. Check log in simpler format Version control is important because it frees you Initialize a repository $ git log --oneline to try new ideas with your code, without worrying that $ git init dc2ebd6 (HEAD -> main) Greets uer. you’ll break your overall project. A distributed version bf55851 Started project, everything works. Initialized empty Git repository in control system like Git is also really useful in working my_project/.git/ collaboratively with other developers. Exploring history Installing Git Checking the status You can explore a project's history by visiting specific It's important to check the status of your project often, even commit hashes, or by referencing the project's HEAD. HEAD You can find an installer for your system at git-scm.com/. refers to the most recent commit of the current branch. Before doing that, check if Git is already on your system: before the first commit. This will tell you which files Git is planning to track. Visit a specific commit $ git --version git version 2.30.1 (Apple Git-130) Check status $ git checkout b9aedbb $ git status Return to most recent commit of main branch Configuring Git On branch main $ git checkout main No commits yet You can configure Git so some of its features are easier to Untracked files: use. The editor setting controls which editor Git will open Visit the previous commit .gitignore when it needs you to enter text. hello.py $ git checkout HEAD^ See all global settings ... Visit an earlier commit $ git config --list Adding files $ git checkout HEAD^^^ Set username You'll need to add the files you want Git to keep track of. Visit the previous commit $ git config --global user.name "eric" Add all files not in .gitignore $ git checkout HEAD~1 Set email $ git add . Vist an earlier commit $ git config --global user.email Add a single file $ git checkout HEAD~3 "[email protected]" $ git add hello.py Set editor Learning more $ git config --global core.editor "nano" Making a commit You can learn more about using Git with the command git When making a commit, the -am flag commits all files that help. You can also go to Stack Overflow and search for git, Ignoring files have been added, and records a commit message. (It's a and then sort the questions by number of votes. To ignore files make a file called ".gitignore", with a leading good idea to check the status before making each commit.) dot and no extension. Then list the directories and files you want to ignore. Make a commit with a message Python Crash Course $ git commit -am "Started project, everything A Hands-on, Project-Based Ignore directories works." Introduction to Programming __pycache__/ 2 files changed, 7 insertions(+) create mode 100644 .gitignore ehmatthes.github.io/pcc_3e my_venv/ create mode 100644 hello.py Branching Stashing changes Git & GitHub When the work you're about to do will involve multiple If you want to save some changes without making a commit, GitHub is a platform for sharing code, and working commits, you can create a branch where you'll do this work. you can stash your changes. This is useful when you want to collaboratively on code. You can clone any public project on The changes you make will be kept away from your main revisit the most recent commit without making a new commit. GitHub. When you have an account, you can upload your branch until you choose to merge them. It's common to You can stash as many sets of changes as you need. own projects, and make them public or private. delete a branch after merging back to the main branch. Stash changes since last commit Clone an existing repository to your local system Branches can also be used to maintain independent releases of a project. $ git stash $ git clone Saved working directory and index state https://github.com/ehmatthes/pcc_3e.git/ Make a new branch and switch to it WIP on main: f6f39a6... Cloning into 'pcc_3e'... $ git checkout -b new_branch_name ... Switched to a new branch 'new_branch_name' See stashed changes Resolving deltas: 100% (1503/1503), done. $ git stash list See all branches stash@{0}: WIP on main: f6f39a6... Push a local project to a GitHub repository You'll need to make an empty repository on GitHub first. $ git branch stash@{1}: WIP on main: f6f39a6... main ... $ git remote add origin * new_branch_name https://github.com/username/hello_repo.git Reapply changes from most recent stash $ git push -u origin main Switch to a different branch $ git stash pop Enumerating objects: 10, done. $ git checkout main ... Switched to branch 'main' Reapply changes from a specific stash To https://github.com/username/hello_repo.git $ git stash pop --index 1 * [new branch] main -> main Merge changes Branch 'main' set up to track remote branch $ git merge new_branch_name Clear all stashed changes 'main' from 'origin'. Updating b9aedbb..5e5130a $ git stash clear Push recent changes to your GitHub repository Fast-forward hello.py | 5 +++++ $ git push origin branch_name Comparing commits 1 file changed, 5 insertions(+) It's often helpful to compare changes across different states Delete a branch of a project. Using pull requests When you want to pull a set of changes from one branch $ git branch -D new_branch_name See all changes since last commit into the main branch of a project on GitHub, you can make Deleted branch new_branch_name a pull request. To practice making pull requests on your own $ git diff (was 5e5130a). repositories, make a new branch for your work. When you're Move last commit to new branch See changes in one file since last commit finished the work, push the branch to your repository. Then $ git diff hello.py go to the "Pull requests" tab on GitHub, and click "Compare $ git branch new_branch_name & pull request" on the branch you wish to merge. When $ git reset --hard HEAD~1 See changes since a specific commit you're ready, click "Merge pull request". $ git checkout new_branch_name $ git diff HEAD~2 You can then pull these changes back into your local main $ git diff HEAD^^ branch with git pull origin main. This is an alternative Undoing recent changes $ git diff fab2cdd to merging changes to your main branch locally, and then One of the main points of version control is to allow you to pushing the main branch to GitHub. go back to any working state of your project and start over See changes between two commits from there. $ git diff fab2cdd 7c0a5d8 Practicing with Git Get rid of all uncommited changes See changes in one file between two commits Git can be used in simple ways as a solo developer, and complex ways as part of a large collaborative team. You can $ git checkout . $ git diff fab2cdd 7c0a5d8 hello.py gain valuable experience by making a simple throwaway Get rid of all changes since a specific commit project and trying all of these steps with that project. Make $ git reset --hard b9aedbb Good commit habits sure your project has multiple files and nested folders to get Try to make a commit whenever your project is in a new a clear sense of how Git works. Create new branch starting at a previous commit working state. Make sure you're writing concise commit $ git checkout -b branch_name b9aedbb messages that focus on what changes have been Weekly posts about all things Python implemented. If you're starting work on a new feature or bugfix, consider making a new branch. mostlypython.substack.com