Push, Pull, and
Git Into It

Git Workshop & Information

JJ Bastida

General Info

Before we start the lesson...

Be sure to download and install Virtual Studio Code and Git.

In this case having VS Code will make your life a tad bit easier because of the built in terminal. You can however just use the git bash terminal just the same.

You need to install git to actually have and use git.

Make sure you also have a Github account or equivalent git program account.

We're going to be using it, as it is an open source friendly and free git hosting service.

Have some sort of version based project in mind when using git, you can use git with any kind of file, but generally git shines with code.

Where do I Git?

Time to git it and checkout everything

"Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency." - Git

It was made as a solution to many pre-existing version control systems that didn't effectively keep teams working synchronously and effectively.

It works by storing "snapshots" of your project in a very clean and easy to understand flowing branch structure.

It works with any kind of file, and if used properly can save you time and space when working on group projects.

Git ideally is stored locally and on some sort of remote service, with your local files containing all of the things that you are currently altering and the "main" (aka. master) stored elsewhere safe.

The process of editing files starts with branching from the main, which in a sense is creating a perfect copy of the main to work off of so that you don't break anything that is live and working. Although you can work on the main that would kind of defeat the purpose of git.

Using Git - Setup

Pull! Commit! Stage! Push!!!

Before anything you need to decide on a project that you are going to be working with, you can start with an empty folder but you need something to start with. I would suggest a blank template folder if anything with some lorem ipsum.

Now you need to create a repository on Github, to actually store your information.You can do that here

Once created, you will be prompted to actually add things to the repository, or to create one somewhere on your computer.

If you choose to add from an existing repository, go to your folder with all of your information and run the first code block on Gitbash from that location. Usually a secure file location like in your C:/ drive in a new folder called git is best.

If you choose to create a new repository folder, go to a secure location on your pc, in some sort of Git folder. Once there start gitbash and initialize the spot with the second code block.


echo "# ******" >> README.md
git init
git add README.md
git commit -m "Initial Commit"
git remote add origin https://github.com/******
git push origin ******

Be sure to change the location and names to the appropriate ones (Don't use ******)


git remote add origin https://github.com/******
git push origin ******

Actually using the Git

I don't think you're ready for this jelly!

Let's start by branching off of the current main; branching allows us to make changes and commit without ruining the current main.
git checkout -b NewBranch

Now actually make some changes to code you're working on. You are now currently working on a different branch, essentially a "snapshot" of the main.

Once you are satisfied with the changes on your branch, it is time to add/stage the changes. Staging is just the phase before committing; it's preparing your individual files to be committed. The following will stage all of the changes you have made. git add -A

Finally is the commit, a commit bundles all of the changes together as a new snapshot or "version" of your branch. You can have hundreds or thousands of commits before pushing to the main. To commit, type the following with some sort of description to help you know what you changed. git commit -m "This message and the -m are optional"

Once you have completed all of your commits, you need to push the current branch into the main, then merge the branch into the main so that the main gets everything that is in your branch.

First, to push your branch to Github, you need to be sure you are on your branch with nothing left to commit; then write the following. git push origin NewBranch

Once pushed there are two ways to merge the main with the branch, if you have the adequate permissions, you can do it in the code with the following. git checkout main
git merge NewBranch

This can also be done on Github through merge requests. You can create a merge/pull request from the branch you just pushed, simply go to your git repository on Github. Select new pull request, select which branch you want to merge, and create the request. If you have the adequate permissions on the repository you can accept the merge/pull request and the branch will become the main.

Git Keywords

Ch-ch-ch-checkout that branch

checkout - Used to switch branches and go to another branch.

pull - Gets all of the branches & commits from either the main or another branch, it will also rebase and clean up heads, to point your branch in the right direction.

push - Sends all of the current commits and branches on the current branch to a source, generally the origin.

add - Stages your changes and prepares them to be committed.

commit - Takes your staged changes and bundles them together as a current version of the branch, it generally includes a message description along with it.

origin - The source location of all of your branches and your main. In this case it would be your repository on Github.

merge - Takes the commits on another branch and merges them into the current branch.

stash - A temporary storage that saves changes that you may not want to stage or commit at the moment.

clone - A complete copy of the entire repository, saved into a location on your computer.

log - Shows a log of all of the previous commits and changes that were made to the branch.

remote - Shows which repositories your current branch/project is connected to, and where pushing or pulling will grab from (aka the remote Github repo/origin).

alias - It is essentially a way to write your own custom git shortcuts, you can assign aliases to longer git commands to make them quick and easy to write.

Help & Troubleshooting

HELP! EVERYTHING IS A BRANCH AND THERE ARE MERGE CONFLICTS!

When merging, be sure that you are on the right branch, and that you are not merging the wrong thing into the wrong branch.

DO NOT COMMIT DIRECTLY INTO THE MAIN, it will get messy really quickly.

If you have made a mess of commits, consider the cherry-pick command, it lets you rewind and grab specific commits that you want to keep.

Try not to branch off of a branch, and try not to have multiple people working on the same thing on the same branch from different places.

You cannot continue to merge something if there are merge conflicts. ALWAYS RESOLVE MERGE CONFLICTS AS THEY APPEAR AND THEN RESUME THE MERGE. To view conflicts use git status

Once all of your conflicts have been resolved, simply restage all of your changes, and use git merge --continue

If something is being stubborn because the main is behind or your branch structure has become messy, you can force the merge with --ff (fast-forward), this is very dangerous however, as it essentially forces the main to wherever you are pointing it, but may be of use at times.

Tips

For all you cool kids 😎

Try to commit as often as possible and write very clear and easy to understand description messages.

Create a bunch of aliases to improve your workflow.

Try to keep the number of branches you have to a minimum, it helps to stay organized and to not have too many open old versions of your project.

Name your branches in a consistent way, and keep your naming structure easy to read.

Keep open communications with teams, and always divide the work in a way where you can each be working on different non-overlapping parts of the same project.

Use your code editor and the Gitlens plugin, it helps see changes, branches, and more, much more clearly.

Avoid absolutely destroying a branch by making way too many commits. Once you have completed the goal of the branch, GET IT INTO THE MAIN AND RID OF IT.

If you are on a team, remember to pull from the origin before pushing to the origin. This is so that you are always up to date before pushing.

That's all for now!

Be sure to contact me if you have any questions or concerns