Git & Github
Version Control for coding. Here I will throw in commands I commonly use in my projects.
1. Starting a Project
Creating a New Project
Create a new directory and navigate to it
Initialise Git versioning for the project (this will create a .git directory)
Map the project to an existing remote Github repository
Making Changes to an Existing Project as a Collaborator
Download a copy of the repository locally on your computer. There are 3 main ways to do this:
1.1. Via HTTPS (requires Github login/password)
1.2. Via SSH (requires ssh key)
1.3. Via Github CLI
2. Creating Local Changes
Shows list of changed files
Shows differences in changed files. Note adding
--staged
shows differences in staged (after git add) files
Shows the commit history
Commit your added files
If you made a mistake, you can use git reset
to unstage changes.
Adding a filepath argument will only unstage that particular file
If you want to completely discard all changes and reset to the last commit you can either
3. Publishing your Local Changes
After committing changes from section 2, your changes will only be reflected in your local computer. If you want to push those changes to the online public Github repository so your team can use your changes, you have to run
4. Updating the Source Code
If a team member has pushed changes and you want to update your code, then you can choose to:
Download the updated changes (without modifying your current local code)
Download the updated changes and updates your current local branch code with the new \ code
If you made current local changes and still want to pull in the new code, you can either choose to commit the changes (Section 2), or stash them. Stashing will keep a copy of your code and reset your code to the last local commit (so you can pull in the new code without conflicts)
If you want to retrieve the stashed code, you can
To find what exists in the stash, you can run
To empty the stash, you can run
5. Branching
Work in branches to develop a feature, then merge with master via a pull request
To list all local branches:
To delete (force) a branch, add the -D flag
To create and navigate to a new local branch
6. Other Useful Commands
If you have updated .gitignore to ignore already tracked files, you can type the following to stop tracking the file.
7. Setting up Git/Github on a New Computer
Install Git
Configure Git
You can view your current configuration by
You can update your email and name (for your commits) by:
Last updated
Was this helpful?