- Understand how to take a real project and integrate it with Git/GitHub.
- Practice basic pull requests and resolve basic merge conflicts.
- Use the GitHub Desktop tool to see your changed files and make commits.
Think of 3 challenges you have faced when using Git or GitHub.
Then, share with a partner and compare/contrast your experiences.
Version control is software that helps a software team manage changes to source code over time. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members.
Source: Atlassian Git Tutorial
Version control allows you to:
- Review previous versions of your code
- Revert to a previous version in case of errors
- Experiment with new features without fear of breaking your code
- Collaborate effectively & split up work without blocking each other's changes
A centralized file system is like a bank: There is one central "source of truth" with a ledger of all changes made.
GitHub is more like Bitcoin: Every person can have their own version of a repository that is equally valid!
How do we do the following in Bash?
- View the current file path?
pwd
- Make a new directory?
mkdir name_of_directory
- Navigate into (or out of) a directory?
cd name_of_directory
orcd ..
- See all files in the current directory?
ls
- Create a new file?
touch file_name
Here is an example of how to initialize a Git repository:
$ git init
$ git remote add <remote_name> <remote_url>
And how to make changes and push them to GitHub:
$ git add <your_file_name>
$ git commit -m "Update server code and add 2 routes"
$ git push <remote_name> <branch_name>
git init
will initialize a Git repository inside of the current directory. We can prove it was initialized by usingls -a
to see hidden files.git remote add origin
adds a remote (short for "remote repository") named origin.git add
is like adding our files to a shopping cart. We may not want to check out just yet - this allows us to still make some changes before pushing.git commit
makes a commit to our local repository containing our changes. It still won't be reflected in GitHub!git push
sends all of the commits we've made to the remote we added. The remote name by default is "origin", and the branch name by default is "master".
Usually, the remote name is called "origin" and the branch name is called "master". It's good to follow this convention unless you have multiple remotes or branches.
You can pull in changes someone else has made with:
$ git pull <remote_name> <branch_name>
Another way to initialize a repository is to clone an existing one.
$ git clone <remote_url>
Note that if you are not the repository's owner (or a collaborator), you will not be able to push your changes! Instead, create a new, empty GitHub repository and add it as a remote with git remote add
as shown above.
There are a few useful commands to get the "lay of the land" and aid in debugging:
git status
- Run before and/or after adding filesgit log
- shows previous commitsgit reflog
- shows all changes made to the repository (not just commits)git diff
- shows diffs in untracked files
Add a file called .gitignore
to specify any files that you don't want to be tracked. For example, my .gitignore
file might include the following contents:
env/
__pycache__
.DS_STORE
Check out gitignore.io to automatically generate a .gitignore file!
If you haven't yet, initialize a repository in your BEW1.1 Homework folder, and follow the steps to push your changes to GitHub.
Merge conflicts occur when you attempt to pull in changes that conflict with (i.e. change the same lines as) your changes.
They are a normal part of working with Git and it's good to practice them!
In your homework directory, change a few lines of code, add, and commit (but don't push).
Then, go to the corresponding GitHub repository and change those same lines of code in a different way.
Try git pull origin master
. You should get a merge conflict! See if you can resolve it.
Pair up with someone you haven't worked with yet.
Clone your partner's repository. Then, both make changes to the same lines of code and try to push your changes. See if you can resolve the merge conflict!
After resolving, commit and push your final changes. Now you should both see the same code.
GitHub Desktop is a desktop client that takes a lot of the pain out of using command-line Git. It will help you do all of the basic commands - add, commit, and push - and will give you visual reminders of what changes you have made.It is...
- Easy and straightforward to use
- More visual
- Shows side-by-side file diffs
- Homework 1 - Due Monday at midnight. Link your repo in the Progress Tracker to submit!
- Atlassian Git Tutorial - highly recommended if you want a deeper dive into Git, but most of it is not necessary for your day-to-day work
- Fixing Common Mistakes (video)
- MS Branching tutorial
- Info in commit messages
- More sources on resolving merge conflicts