Download as KEY, PDF, TXT or read online from Scribd
Download as key, pdf, or txt
You are on page 1of 18
INTRO TO GIT
HOW TO WORK TOGETHER WITHOUT HATING
EACH OTHERS' GUTS WHAT IS GIT? Distributed Version Control System This means that every machine has its own copy of the repository De facto standard of VCS in the industry GITHUB One of the cloud hosts of git repositories Collaborate with other developers across the world We’ll host our central repositories here REPOSITORY Two types: Local and Remote Local repository is the one that lives in your machine Remote repository is the one that is hosted somewhere else (like Github) Git repositories are housed in .git folder This folder has information that keeps track of commit history of different branches of your local repository And other information, such as remote repository location CREATING A GIT REPO 1. git init to create an empty local repository in one of your folders 2. Then create an empty remote repository in your git cloud host of choice 3. git remote add <short-name> <url-to-your-remote-repo> to add remote repository information to your local repository You can also create an empty remote repository first git clone <url-to-repo> to clone the remote repository to your local machine WORKING WITH REMOTE REPOS git pull and git push to sync your local and remote repositories. Pull to bring remote repo’s changes into your local repo Push to update remote repo with your local repo’s changes STAGING A temporary place for your changes before you “save” them to your repository Only the changes in staging will be committed Add files to the staging via git add <path-to-file-or-folder> View the current state of staging with git status COMMIT Committing saves changes on staging to the repository Each commit has its own unique identifier So you can track changes and rollback if necessary Create a new commit with git commit with a meaningful message about this commit See the commit history with git log .GITIGNORE Folders and files listed in this file will be ignored by git, aka git will not track changes to these files Examples of folders/files you should add to .gitignore: Files that change every time you compile/build (ie. build outputs) Sensitive information (ie. your DB connection string) Have this file at the same level as your .git folder GIT WORKFLOW 1. Initialize a repository and add remote or clone from a remote repo 2. make a change 3. git add (to staging) 4. git commit (to local repository) 5. git push (to remote repo) 6. (When you want to sync from the remote repo) git pull BRANCH Branch allows multiple developers to collaborate over a source code. Each project has one Main branch This branch is the sacred branch, tread lightly Each developer checks out from the main branch to work on their own feature Branch per feature, not per developer Name your branch wisely BRANCH To see all your branches in your repo git branch To go to another branch git checkout <branch-name> To create a branch git branch <new-branch-name>: creates a new branch git checkout –b <new-branch-name> : creates a new branch and checks out to the branch MERGING BRANCHES Again, two ways. 1. Directly merging one branch to another Quick and dirty, use this if you’re merging two local branches together git merge <branch-name> git pull <remote-repo> <remote-branch-name> will also merge the remote branch to your local one 2. Raising a Pull Request (PR) Preferred method if you’re trying to merge a feature branch to either dev or main branch Creates a PR on a remote repository that allows other people to review your code for potential issues and merge conflicts MERGE CONFLICT When merging two branches, git tries its best to figure out which code goes where. But sometimes, it needs human intervention to merge correctly without devastating consequences git marks the snippet of the code that needs human attention with both versions (local, incoming) Humans manually review the snippets and decide how that code should look like It’s tedious but not a scary job. BEST PRACTICES Commit and Sync often Small, frequent commits will save your life one day Make sure your commit messages are meaningful Push and pull from remote repository often Start with the .gitignore file Main is Sacred When collaborating, avoid touching main directly, use PR’s instead QUESTIONS? DISCUSSION Q’S What is git? Git vs github? What is repository? How do I create one? Tell me a basic workflow of making changes to the code and then committing to the repo What are branches? What is gitignore? What do you typically put in the gitignore? RESOURCES Git Cheat Sheet Git official doc For those git oh**** moments