Git Intro
#
WhyWe cover Git because it is a Distributed Version Control System. Distributed means there is no central server required, so work can be done “Offline” simultaneously with someone else also working offline. In the past, with traditional Centralized Version Controls Systems, two people editing a file was very difficult and sometimes whomever saved the file last would overwrite someone else's work.
Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development and is a free and open-source software.
While we are using Git, we will be using the command prompt / terminal a lot. Here are a couple of great cheat sheets for Git and GitHub commands:
#
WhatVersion Control is a means for managing your source code
Version Control allows you to:
- Access previous versions of your code (History of content changes)
- Have multiple working copies of your code
- Backing up your code is now trivial
- Share your work with other people easily and facilitates collaborative changes to files
Version Control Systems are usually represented with a tree data structure
#
GitA distributed version-control system (DVCS) for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files.
Git != GitHub: (Git is not the same as GitHub)
So while Git is a tool to manage your source code history, GitHub is a hosting service for Git repositories
#
Git and GitHubGit is the tool, GitHub is a service for projects that use Git
GitHub is where our Remote repository will live. Our computer is where our Local repository will live. Once we have our project in a working state, we can push what’s in our Local repository to our Remote repository.
#
Making CommitsThe traditional software expression of "saving" is synonymous with the Git term "committing". A commit is the Git equivalent of a "save".
The git commit
command captures a snapshot of the project's currently staged changes.
Each circle in the above diagram is a commit – a snapshot of the project
#
Remote repositoriesVersions of your project that are hosted on the Internet or network somewhere (like GitHub!).
Collaborating with others involves managing these remote repositories and pushing and pulling data to and from them when you need to share work.
With Remote Repositories we usually use these git commands:
git clone:
git push:
git pull:
#
Staging with git addThe git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit There are two ways to add a file(s) to the staging area:
git add <filename>
- add a specific file to the staging areagit add .
- add all modified files to the staging area. the period is what chooses all files
#
Moreover on StagingStage lets you group related changes into highly focused snapshots before actually committing it to the project history. This means you can make all sorts of edits to unrelated files, then go back and split them up into logical commits by adding related changes to the stage and commit them piece-by-piece.
#
WorkflowDeveloping a project revolves around the basic edit/stage/commit pattern.
Edit - Do work (edit your files in the working directory and save)
Stage - When you’re ready to save a copy of the current state of the project, you can stage all your changes with:
Commit - After you’re happy with the staged snapshot, you commit it to the project history with:
Push - In addition to git add and git commit, a third command
git push
is essential for a complete collaborative Git workflow.git push
is utilized to send the committed changes to remote repositories for collaboration. This enables other team members to access a set of saved changes.
#
Basic Terminal/Command Prompt Commands#
Unix/Linux Commandsls
- lists available directories for you tocd
tocd directoryName
- change current directory to the specified directorytouch fileName
- Create a file named fileNamepwd
- prints your current directory
#
Command Prompt(Windows) Commandsdir
- lists available directories for you tocd
tocd directoryName
- change current directory to the specified directoryecho example > fileName
- Creates a file namedfileName
and writes the stringexample
insideecho %cd%
- prints your current directorycode .
- opens an instance of VS Code at your current directory
#
Basic Git CommandsCommand | Definition | Example |
---|---|---|
cmd-shift-. | Show hidden files (MacOS Only) | |
git init | initializes a new repository in the current directory | |
git status | The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git | git status |
git add <fileName> | stages a file so it is ready to commit | git add Algo.txt |
git add . | stages all files in the directory so they are ready to commit | git add . |
git commit -m "this is my first commit" | commits the changes in the currently staged files and includes a message | git commit -m "a descriptive message" |
git push | tells git that you're ready to push your local changes. Just tell git where to push | |
git push <remote-name> <branch-name> | pushing the test branch to the remote repo named 'origin' | git push origin testBranch |
git push -u origin main | setting the upstream flag on the main branch, that way from now on, we can just use 'git push' | git push -u origin main |
git clone <remote url> | copy a remote repository to your local machine. Very collaborative | |
git clone https://github.com/someFolder/some/File.git # "downloading" from github | ||
git branch | lists the branches in the repository | |
git branch <name> | creates a new branch | git branch myBranch |
git checkout <name> | switches to a specific branch | git checkout myBranch |
git checkout –b <name> | creates a new branch, and switches to that branch at the same time | git checkout -b myOtherBranch |
git merge <name> | merges a specific branch into the current branch | git merge myBranch # When merging make sure you are on the branch you want the other code to merge into # If right now we are on the 'main' branch, we would be merging 'myBranch' into 'main' |
git pull | downloads content from a remote repository and immediately update the local repository to match that content | |
git log | display our commit history | |
git diff | enables you to compare changes in the working directory against a previously committed version | |
git config --global user.email "<you@example.com]>" | configures an email address for your commits | |
git config --global user.name "Your Name" | configures a name for your commits |
#
SourcesLearnEnough - Getting Started Documentation and Tutorial
#
Takeaways- Git is a tool for version control management
- Github is a service for hosting our code base and git history
- The git workflow is edit, add, commit with message, and push