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 - file that added and ready to commit in next commit.
- Working copy - a cloned repository from
git clone
command then working at it. - master - default branch name of repository that come from cloning any repository
- HEAD - pointer that reference the parent of next commit that going to created
-
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 = "Tezigudo" git config --glabal user.email = "[email protected]"
-
There are 2 ways to create a local Git repository. What are they?
- first way
git init #creating .git in current directory git add filename git commit -m "commit message"
- second way
git clone URL local_dir_name
-
When you create a git repository by entering
git init
, Git will create a "hidden" directory for the local repository. Where is the directory for this local repository (relative to the directory where you typed "git init")?.git
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 files in the staging area.
git diff --name-only --cached
-
Remove
README.md
from the staging area. (Useful if you accidentally add something you don't want to commit.)git restore --staged README.md
-
Commit everything in the staging area to the repository.
git commit -m "commit message"
-
Describe 2 steps to configure the repository so git will ignore all files in the
out/
directory:- step one create file name
.gitignore
all name in this file will be ignore to commit. - step two in the
.gitignore
file write aout/
in it
- step one create file name
-
Command to move all the .py files from
src
to the top-level directory of this repository, so they are also moved in the Git repo.git mv src/*.py .
-
Commit this change with the message "moved src directory":
git commit -m "moved src directory"
-
Command to add all changed files (but not untracked files) to the staging area using a single command.
git add -u *
-
Delete the file
c.py
from your working copy and the repository:git rm src/c.py
-
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 --cached 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 filename(optional)
-
Undo "git add": If
main.py
has been added to the staging area (git add main.py
), remove it from the staging area:git restore --staged 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 restore 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)aaaa ---> bbbb ---> cccc ---> dddd [HEAD -> master]
The command to reset HEAD and master to the commit id
bbbb
:git reset --hard 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
-
List all the files in the current branch of the repository:
git ls-files
example output:
.gitignore README.md a.py b.py test/test_a.py test/test_b.py
-
Create a new branch named
dev-foo
:git branch dev-foo
-
Display the name of your current branch:
git status -b
-
List the names of all branches, including remote branches:
git branch -r
-
Switch your working copy to the branch named
dev-foo
:
- Soln1
git checkout dev-foo
- Soln2
git switch dev-foo
-
Merge: To merge the work from
dev-foo
into the master branch, perform these steps:- step one
git switch master
- step two
git merge dev-foo
- step one
-
Describe under what conditions a merge may fail.
merge fail come from merge conflict that mean same file between 2 branches have been edited the same part, So git won't merge cleanly.
My favourites command is git checkout
or git switch
in new version. this command will switch all file in the choosen branch to working copy and switching head to choosing branch.
- my favourite git resources is Pro Git Online Book and Git Visualizer
Pro Git Online Book Chapters 2 & 3 contain the essentials. Downloadable PDF is also available.
Visual Git Reference one page with illustrations of git commands.
Try Git:
Learn Git Interactive Tutorial excellent visual tutorial.
Git Visualizer execute Git commands in a web browser and see the results as a graph.