Git Basics - nmontierth/fullstackOpen GitHub Wiki
GIT
Two categories of Version Control Systems-- Centralized, Distributed
Centralized-- all team members connect to a single server and get their code from that server (single point of failure). Distributed-- every member has a local version of the code. Git is a distributed version control system.
Using GIT-- via command line, through code editors and IDEs, GUI tools
Most programmers use a combination of GUI tools and the command line (there are limitations of GUI tools).
Configuration
To start, it's important to configure GIT after it is downloaded locally.
Configuration is done in the GIT BASH terminal. The following configurations should be done.
git config --global user.name ""
git config --global user.email
git config --global core.editor "code --wait" <-- makes VS code the core editor
git config --global -e
git config --global core.autoclrf true
More info on config can be found by running:
git config --help or git config -h
Initializing a repository
cd to the project folder. Run git init in the Git BASH terminal. Deleting a git repository: Run rm -rf .git in the Git BASH terminal.
Workflow
Git Workflow:
Three areas-- project directory, staging area, and the git repository. How it works: Let's assume the project directory, staging area and repository are all empty. We then create two files within the project directory, file1 and file2. These files exist solely in the project directory. TO add them to the staging area, we run the command git add file1 file2. Now, these files are both in the project directory and the staging area. To pass them to the repository, we run git commit -m "meaningful message". This commits whatever is within the staging area to the repository. At this point, these 2 files now exist in all three areas (once committed, files remain in the staging area). Say we decide to delete file2 from our project directory. To also remove it from the staging area, run git add file2 Now, both the staging area and project directory only contain file1. We can commit this to the repository by once again running git commit -m "message".
Each commit stores the full content of the project along with an ID, message, date time, author and complete snapshot.
Staging files
git status --> shows which files have been committed, etc. git add *.txt --> every .txt file git add . --> every file in the directory
Committing changes
git commit --> opens up a more detailed, and editable commit message in IDE Suggestion: commit often
Skipping the staging area:
git commit -am "message" Not recommended.
Removing files
First Method: rm file2 --> removes from project directory git add file2 --> removes from staging area git ls-files --> displays files in staging area git commit -m "message" Second Method: git rm file2 --> removes from directory and staging area git commit -m "message"
Renaming and Moving Files
First Method: mv file1.txt main.js --> move the contents of file1.txt to main.js (same as renaming the file) git add file1.txt --> stages the deletion of file1.txt git add main.js--> adds main.js to the staging area Second Method: git mv file1.txt main.js --> stages the renaming of file1.txt to main.js git commit -m "message"
Ignoring Files
Certain files and directories should be ignored in the repository so that changes to them do not constantly need to be commmitted. This is done by creating a file called gitignore, do this by running: .gitignore To add files to .gitignore, run: echo logs/ > .gitignore Add .gitignore to the staging area and commit.
For this to work, the logs directory cannot be within the staging area. Delete a file solely from the staging area by running: git rm --cached -r logs/
Short Status
git status -s
Displays two columns-- the left one is the staging area, the right is the project directory. M --> modified A --> added ?? --> not added yet to staging area Green --> in staging area Red --> not yet in staging area
Viewing Staged and Unstaged Changes
git diff --staged --> difference between staged and committed
git diff --> difference between working directory and staged
Visual Diff Tools
KDiff3
P4Merge
WinMerge
VSCode
Configuring Visual Tools:
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
git config --global -e
git difftool --staged
git difftool
Viewing the History
git log --> shows the history of commits
git log --oneline (--reverse --> reverses the order in which commits are viewed) --> one line version of commit history
git show ID --> viewing the contents (difference) of a specific commit
git ls-tree ID --> shows all contents of a commit (the whole snapshot)
git show (ID of file as output from the above command) --> shows the content of a specific file within a commit
Unstaging Files
git restore --staged file1 --> removing a file from staging area
Discarding Local Changes
git restore file1 --> copy from staging area to working directory
git clean -fd --> removes untracked files (undo local changes entirely)
Restoring a File to an Earlier Version
git log --oneline --> shows all previous commits
git restore --source=(FROM ABOVE) filename