- Version control
- Collaboration
Multiple versions/snapshots of the same file (version control) not possible with basic storage systems
- DVCS = Distributed Version Control System
- Everyone has the same copy of the repo
- SVN = Subversion (another version control system, not used so much anymore)
- Centralized: Repos are not shared, and branching is hard
- Commits (your snapshots) should not be the same as pushing to the server. In SVN, a commit is a push.
- With Git, one push can push multiple commits.
Analogy: Like a checkpoint or save point.
DEFINITION: Commit
A commit is a name for a collection of file changes. We use that as an opportunity to create a snapshot. Commits do not store files - commits store CHANGES to files.
$ git add -A -> # add all changes to commit
$ git add . -> # add all changes except file deletions to commit
$ git add filename.txt # "Add the changes to filename.txt to the next commit"
Whenever you git add a file, the changes to the file are added to the staging area. Commits capture all the changes stored in the staging area. Files themselves are not stored in the staging area, only the file "deltas" (changes).
"Untracked files" are files in the working tree that have not been added to the repo at all.
$ git status
$ git reset HEAD - It removes the changes to a file from staging area, but it does NOT undo the actual changes.
$ git diff
The main use case for git diff is to see if your change set is getting too large, and it's time to start committing.
- Commit - A named collection of changes to files
- Delta - Change
- git status
- what changes (if any) to files are in the working directory
- what changes (if any) are in the staging area
- Staging area - A temporary storage area that contains file changes designated to be committed on the next commit
- git add
- Takes changes to files and "moves" them to the staging area (moves?)
- git diff
- Without a filename specified, it displays all unstaged changes for all files since the last commit
- with filename, it displays unstaged changes for that specific file
- git log
- Summary of commits
- Most recent on top
- You can scroll through them using arrow keys
-
Copy a file in command line
$ cp origin destination
-
Rename a file in commandline
$ mv origin destination
Creating a patch:
$ diff -u original_file new_file > file.patch
Diff files in the working tree with their corresponding files at the HEAD. Store the resulting patch in the staging area.
-
Can you be impersonated by someone who has your public key?
- No - it's public, it's safe to share online.
-
git clone
- Downloads the remote repository to the designated directory and checks out the master branch
- It automatically links the local repo to the remote repo
-
git remote
- Mostly used with
git remote add
- git remote add alias git_repo_url
- Create a reference to a remote repository named remote_repo_alias
- origin is the conventional name for the default repository
- Mostly used with
HEAD - A special alias for the most recent commit for the branch
git push * Uploads and applies commits to the HEAD of the remote repository. * Only the commits created in the local repository since the last push are uploaded
git pull * Downloads and applies commits to the HEAD of the local repository * Only the commits created in the repository since the last pull are downloaded
- Master should always be production ready
- Branches are names for a collection of commits
The process:
$ git checkout -b branch_name
- creates a new branch called branch_name
- checks out the branch
PULL BEFORE YOU PUSH!!!!!!!
git branch
- shows all local branchesgit branch -a
- shows you all local and remote branches
$ git push -u origin branch_name
- Take our current branch, push our commits to the repo at origin on the branch branch_name.
- -u => --set-upstream => links the remote branch to the current local branch the command was run on
- this means we don't have to specify
git push origin branch_name
on this branch. We can just dogit push
now.
$ git checkout branch_name
NOTE: git checkout -b branch_name
=> create branch called branch_name and immediately check it out
- From repo on Github, go to graphs => Network
- checkout master
- pull latest master
- checkout feature_branch
- merge master in feature_branch
- resolve conflicts if necessary
- checkout master
- merge feature_branch in master
$ git merge
git merge branchname
=> merge branchname into currently checked out branch
If there's a merge conflict, use git status to see the files with conflicts After merge conflict, git add the files you resolved Then
$ git commit
(no -m message)
colon wq to get out of VIM and save the commit
-
"I want to undo changes made to a specific file since the last commit"
- Suppose we haven't added the changes to the staging area?
git checkout -- thefile.txt
- What if we have added the changes to the
git reset HEAD thefile.txt
git checkout -- thefile.txt
- Suppose we haven't added the changes to the staging area?
-
"I want to undo all my changes made since the last commit"
git reset --hard
- git is very conservative and does not remove unversioned (untracked) files without you explicitly saying so. This is so you do not lose work.
- equivalent
- DEFINITION: The "Index" is the same as "the staging area"
- RTFM - Git status man page
- Any changes to tracked files are discarded
-
"I want to delete all the untracked files"
git clean
- Usually has to be done
git clean -f
-
"I can't checkout my feature branch because git says it would result in a conflict"
- This arises usually when you're switching back and forth between multiple branches throughout the day
- If the checking out of a branch would not possibly result in a merge conflict, git will bring the staging area and working tree over to the branch
- Otherwise, we can use the "stash" as a temporary storage of the changes introduced to the working tree, as well as the diffs stored in the staging area
$ git stash
stores the aforementioned information into a temporary holding area This frees us to check out the desired branch$ git stash pop
extract the changes from the stash and introduces the changes into the current branch
-
"I know a specific commit introduced a regression and I want to undo the changes made by the commit"
- regression - something was working a one point but now it's not
- git will not let you push to the remote branch if commits are found in the remote
branch that do not exist in the local branch.
- Use
git revert COMMIT_ID
to create a reversion commit. A reversion commit contains the inverse of the diff that represents the commit we are reverting.
- Use
- The COMMIT_IT can be found via
git log
or the list of commits on Github