Basics
Adding and Changing Things
Undo Changes and Recover Files
Viewing Commits
Branch and Merge
Commands for Remotes
Favorites
Resources
In this file, directory paths are written with a forward slash as on MacOS, Linux, and the Windows-Bash shell: /dir1/dir2/somefile
.
-
When using Git locally, what are these? Define each one in a sentence
- Staging area - The area that the modified file was added, prepare to be committed in the next commit.
- Working copy - The local repository that we use for modifying files on the computer.
- master - The main or primary branch of the repository.
- HEAD - The pointer that refer to which commit we're observing.
-
When you install git on a new machine (or in a new user account) you should perform these 2 git commands to tell git your name and email. These values are used in commits that you make:
# Git configuration commands for a new account git config --global user.name "Nanthawat Duang-ead" git config --global user.email nanthawat.boom@gmail.com
-
There are 2 ways to create a local Git repository:
- Initializing the existed local repository using
git init
command - Cloning the repository from remote repository using
git clone <git-url>
command
- Initializing the existed local repository using
Suppose your working copy of a repository contains these files and directories:
README.md
out/
a.exe
src/a.py
b.py
c.py
test/
test_a.py
...
-
Add README.md and everything in the
src
directory to the git staging area.git add README.md src/
-
Add
test/test_a.py
to the staging area (but not any other files).git add test/test_a.py
-
List the names of files in the staging area.
git status
-
Remove
README.md
from the staging area. This is very useful if you accidentally add something you don't want to commit.git reset README.md
-
Commit everything in the staging area to the repository.
git commit -m'commit_message'
-
In any project, there are some files and directories that you should not commit to git.
For a Python project, name at least files or directories that you should not commit to git:- pycache
- venv
- idea
-
Command to move all the .py files from the
src
dir to the top-level directory of this repository. This command moves them in your working copy and in the git repo (when you commit the change):git mv src/*.py .
-
In this repository, create your own
.gitignore
file that you can reuse in other Python projects. Add everything that you think is relevant.
Hint: A good place to start is to create a new repo on Github and during the creation dialog, ask Github to make a .gitignore for Python projects. Then edit it. Don't forget to include pytest output and MacOS junk.
- Display the differences between your working copy of
a.py
and thea.py
in the local repository (HEAD revision):
git diff a.py
- Display the differences between your working copy of
a.py
and the version in the staging area. (But, if a.py is not in the staging area this will compare working copy to HEAD revision):
git diff --staged a.py
- View changes to be committed: Display the differences between files in the staging area and the versions in the repository. (You can also specify a file name to compare just one file.)
git diff --staged
- Undo "git add": If
main.py
has been added to the staging area (git add main.py
), remove it from the staging area:
git reset main.py
- Recover a file: Command to replace your working copy of
a.py
with the most recent (HEAD) version in the repository. This also works if you have deleted your working copy of this file.
git checkout a.py
-
Undo a commit: Suppose you want to discard some commit(s) and move both HEAD and "master" to an earlier revision (an earlier commit) Suppose the git commit graph looks like this (
aaaa
, etc, are the commit ids)git reset --hard bbbb
The command to reset HEAD and master to the commit id
bbbb
: -
Checkout old code: Using the above example, the command to replace your working copy with the files from commit with id
aaaa
:git checkout aaaa
Note:
- Git won't let you do this if you have uncommitted changes to any "tracked" files.
- Untracked files are ignored, so after doing this command they will still be in your working copy.
-
Show the history of commits, using one line per commit:
git log --oneline
Some versions of git have an alias "log1" for this (
git log1
). -
Show the history (as above) including all branches in the repository and include a graph connecting the commits:
git log --graph --all --decorate --oneline
-
List all the files in the current branch of the repository:
git ls-tree -r HEAD --name-only
Example output:
.gitignore README.md a.py b.py test/test_a.py test/test_b.py
The repository currently has the master
branch that is the primary branch of our repository.
- We about to work on a new feature call "X". First, we'll have to establish the new branch
called
feature-X
usinggit branch
.
git branch feature-X
- To work on the branch
feature-X
, we'll have to checkout the branch first using the commandgit checkout
git checkout feature-X
- You just hit your head with the keyboard and barely remember the branch
we are working on. Check the branch you're currently on using
git branch -v
git branch -v
output:
*feature-X
master
- During your coffee break, you cat just somehow mess up your code.
Mr.Muffin accidentally create a new branch call
stupid-human
and manage to commit several time into the branch. Delete the branch bygit branch -d
git branch -d stupid-human
- Finally, your hard work on the
feature-X
branch has come to the end. Merge the branch back to themaster
branch usinggit merge
. But first we'll need to usegit checkout
to go back to themaster
branch.
git checkout master
git merge feature-X
git status
: the command that use for tracking the current state of the directory included the modified file, the file in staging area, or how up-to-date the directory is.
Tip:
-s
or--short
option will show status in the shorter format
git log
: the command that will show the commit-log. The information included date-time of the commit, author information, commit id and the commit message.
- Pro Git Online Book Chapters 2 & 3 contain the essentials. Downloadable e-book is available, too.
- Visual Git Reference one page with illustrations of git commands.
- Markdown Cheatsheet summary of Markdown commands.
- Github Markdown some differences in the way Github handles markdown and special Markdown for repos.
- Git Cheat Sheet The cheat sheet file on the basic git command by GitHub
Learn Git Visually:
- Learn Git Interactive Tutorial great visual tutorial
- Git Visualizer execute Git commands in a web browser and see the results as a graph.