Git
(Fetch)
VCS - a software that tracks & manages changes to files over time.
Repo - workspace used to track and manage files (stored on the cloud)
Git vs. Github:
Git ≠ GitHub.
GitHub makes tools that use Git. It's the largest host of source code in the world.
Install + Setup
You can download Git for free from the following website: https://www.git-scm.com/
After installing, you can see if it's ready to use by checking it's version:
Now let Git know who you are (
global
is used to set the username and e-mail for every repository on your computer):Git Status [info on which changes have/not been staged/tracked]
To see what changes occurred in the staged files:
Git Staging Environment
Repo Files can be in one of 2 states:
Tracked - files that Git knows about and are added to the repository
Untracked - files that are in your working directory, but not added to the repository
As you are working, you may be adding/editing/removing files. But whenever you hit a milestone, you should add the files to a Staging Area.
Git Add [adds a change in the working dir --> staging area]
Git Commit
Staged files ready to be committed to the project repo.
Git Commit [commits stages files --> Repo]
Git Log [view history of commits for repo]
Git Branch
In Git, a
branch
is a new/separate version of the main repositoryBranches allow you to work on different parts of a project without impacting the main branch.
When the work is complete, a branch can be merged with the main branch.
You can switch b/n branches and work on different projects w/o them interfering.
Create Branch
See All Existing Branches
Switch To Existing Branch
Delete Branch (note: can't delete branch currently in)
Rename Branch (must be in the branch being renamed)
HEAD
only 1 branch can be checked out at a time - this is called the HEAD branch, or "current" branch.
The HEAD is the most recent version of a branch
You can find out what HEAD you are viewing by opening the .git/HEAD file in your repo:
Detached HEAD
A detached HEAD occurs when you check out a commit that is not a branch
The term detached HEAD tells you that you are not viewing the HEAD, the most recent version of a branch, of any repo.
An example of entering a detached HEAD state, a point in time when you're viewing a commit that's not th emost recent commti (HEAD), is using git checkout with a hash of a previous commit in history (from "git log"). This is useful if you want to retrieve code you have overwritten in newer commits. Detached HEAD state is not an error nor is it a problem. When you are ready, you can navigate back to the HEAD in your repository
Once you enter into detached HEAD state, you can view and modify the files in a particular commit. This is useful if you want to retrieve a change from a past commit that should be reincorporated into your repo. To save a change from a detached HEAD:
create a new branch (git branch foobar) - this branch shows repo 3 commits back
merge our changes into the main branch (git checkout main; git merge foobar)
Then, we can create a new Git commit with the changes we've collected from our previous ref HEADs.
To Discard Changes in a Detached HEAD,
You do not need to save the changes you make to a detached HEAD.
Once you are done viewing a previous commit, you can go back to the HEAD of your repository. But first, discard the changes you made:
Git Branch Merge
Consider the scenario: We have the emergency-fix ready, and so we want to merge the main and emergency-fix branches.
First, we need to go to the main branch (branch being merged onto)
Now we merge emergency-fix with current HEAD (main) branch
Since the emergency-fix branch came directly from main, and no other changes had been made to main while we were working, Git sees this as a continuation of main. So it can "Fast-forward", just pointing both main and emergency-fix to the same commit.
As main and emergency-fix are essentially the same now, we can delete emergency-fix, as it is no longer needed.
Git & Github
Consider the scenario: you have a local project and you want to create a repo for it
Create repo on website
Copy the https url of the repo
use the below command which says that you're adding a remote repo, with the specified URL, as an origin to your local Git repo.
Now, push the main branch to the origin url, and set it as the default remote branch:
- Pull from Github
When working as a team on a project, it is important that everyone has the most recent changes of the project in their local copy.
With Git, you can do that with git pull, which is a combo of 2 other commands:
fetch - gets all the change history of a tracked branch/repo
merge - combines the current branch, with a specified branch
git pull - used to pull all changes from a remote repo into the branch you are working on.
Consider scenario: you edit the README.md in the repo from the website, and need to get those changes on your local project-folder of the repo. To do this,
- Pull Branch from Github
Consider scenario: you create a new branch in a repo (from Github.com) and now want to see it in your local repo.
pull any changes on remote repo (including addition/deletion of branches)
Usually to see all branches, you would use git branch, but inorder to see all local and remote branches, use the -a flag (all). [If you want to only see remote branches, use the -r flag]
We see that the branch
html-skeleton
is available remotely, but not on our local git. To pull it to your local:
(assume some changes made to readme), stage + commit the readme in the update-readme branch
Now
push
thebranch
from our local Git repo --> GitHub, where everyone can see
- Clone
Now that we have our own fork (only on GitHub), we can also clone the forked repo to our local Git to keep working on it.
clone - full copy of a repository, including all logging and versions of files.
Move back to the original repo, and click the green "Code" button to get the
URL
:
In the terminal, clone the repo (downloads the repo as a folder w/ the name of repo-name)
- gitignore
When sharing your code with others, there are often files or parts of your project, you do not want to share. ie: log files, temporary files, hidden files, personal files
Git can specify which files or parts of your project should be ignored by Git using a
.gitignore
fileGit will not track files and folders specified in
.gitignore
(the file itself IS still tracked by Git)create a
.gitignore
fileNow open the file using VS-Code, and we're just going to add a couple things to skip:
Remote
before you can push anything to Github, you need to establish a remote repo on Github- a destination.
In Git, this destination is referred to as a remote, and is simply a URL of the hosted repo.
Create Remote
adding:
$ git remote add <remote-name> <URL>
By convention, remote-name is "origin"
Copy from Github
pushing:
$ git push <remote-name> <branch-name>
Show Remotes
CHEAT SHEET
Pull Requests
Fork the repository you want to pull-req
Clone your fork to your computer
Create a branch,
git checkout -b temp-branch
PLAYAROUND and save
Add/Commit your changes (in temp-branch)
Change back to main branch
git checkout temp-branch
Merge your newly created branch into main
git merge main temp-branch
Push your changes (from main)
Delete the temporary branch (
git branch -d temp-branch
)Go to your forked repo on GH and open a pull request
Misc
Last updated