Git - 0 To Pro Reference: By: Supersimple - Dev
Git - 0 To Pro Reference: By: Supersimple - Dev
By: supersimple.dev
Tutorial link: https://www.youtube.com/watch?v=hrTQipWp6co
Note: git commands must be run inside the folder that contains all the code.
Creating Commits
In Git, version = commit
Version history = commit history
git init Git will start tracking all changes in the current folder
git status Show all changes since the previous commit
GitHub
Repository = a folder containing code where any changes to the code are tracked by git.
(To create a repository, we create a new folder on our computer, and then run git init)
GitHub = a service that lets us save our git repositories online. It also helps us:
- backup our code in case we delete it on our computer
- see the history of our code changes more easily
- alternatives include Bitbucket and GitLab
git push <remote_name> <branch> Upload a branch of your git version history to your
remote repository
git push <remote_name> <branch> -f Force-push the branch to the remote repository (it
will overwrite what's on the remote repository)
git clone <url> <folder_name> Download the repository and give it a different
folder name
git pull <remote_name> <branch> Update the local branch with any updates from
the remote repository (on GitHub)
git pull origin main Downloads any new commits from the main
branch on origin, and updates the local main
branch with those new commits
git pull origin main --set-upstream
^
Sets up a shortcut so that the next time you are on the main branch and run git pull, it will
automatically git pull origin main
Branching
Branching = create a copy of the version history that we can work on without affecting the
original version history. This lets us work on multiple things (features + fixes) at the same time.
Merging
git merge <branch_name> -m "message" Merge the current branch (indicated by HEAD ->)
with another branch (<branch_name>). Saves
the result of the merge as a commit on the
current branch
Merge Conflicts
<<<<<<< HEAD If there is a merge conflict (git doesn't know
code1 what the final code should be), it will add this in
======= your code.
code2
>>>>>>> branch (This is just for your convenience, the <<<<<<<
and >>>>>>> don't have special meaning)
<<<<<<< HEAD
... <-- Code in the current branch (indicated by HEAD ->)
=======
... <-- Code in the branch that is being merged into HEAD
>>>>>>> branch
1. Delete all the extra code and just leave the final code that you want.
<<<<<<< HEAD
code1
======= => code2
code2
>>>>>>> branch
2. If there are conflicts in multiple places in your code, repeat step 1 for all those places.
3. Create a commit.
git add .
git commit -m "message"
3. Create a pull request on GitHub (a pull request lets teammates do code reviews and add
comments).
4. Merge the feature branch into the main branch (by opening the pull request in the browser
and clicking "Merge pull request")
5. After merging, update the local repository (so that it stays in sync with the remote repository
on GitHub).
git checkout main
git pull origin main
We can either:
1. Resolve the merge conflict on GitHub.
3) Merge main into the feature branch (feature4). Notice the direction of the merge: we want
the merge commit to stay on the feature branch so our teammates can review it.
git checkout feature4
git merge master