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
mkdir my_project && cd my_projectInitialise Git versioning for the project (this will create a .git directory)
git initMap the project to an existing remote Github repository
git remote add origin <[email protected]:<Github-Username>/<Github-Repository>.git>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)
git clone https://github.com/<Github-Username>/<Github-Repository>.git1.2. Via SSH (requires ssh key)
git clone [email protected]:<Github-Username>/<Github-Repository>.git1.3. Via Github CLI
gh repo clone <Github-Username>/<Github-Repository>
2. Creating Local Changes
Shows list of changed files
git statusShows differences in changed files. Note adding
--stagedshows differences in staged (after git add) files
git diff
git diff --stagedShows the commit history
git logStage (add) files to be committed.
.will add both tracked and untracked files,-uwill add only tracked files. Source
git add .
git add -uCommit your added files
git commit -m <your message>If you made a mistake, you can use git reset to unstage changes.
git resetAdding a filepath argument will only unstage that particular file
git reset <filepath>If you want to completely discard all changes and reset to the last commit you can either
git reset --hard origin <branch-name>git checkout .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
git push origin <branch-name>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)
git fetchDownload the updated changes and updates your current local branch code with the new \ code
git pull origin <remote-branch-name>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)
git stashIf you want to retrieve the stashed code, you can
git stash apply <stash-ref>To find what exists in the stash, you can run
git stash listTo empty the stash, you can run
git stash clear5. Branching
Work in branches to develop a feature, then merge with master via a pull request
To list all local branches:
git branchTo delete (force) a branch, add the -D flag
git branch -D <branch-to-delete>To create and navigate to a new local branch
git checkout -b <new-branch-name>6. Other Useful Commands
If you have updated .gitignore to ignore already tracked files, you can type the following to stop tracking the file.
git rm -r --cached .7. Setting up Git/Github on a New Computer
Install Git
Configure Git
You can view your current configuration by
git config --list --show-originYou can update your email and name (for your commits) by:
git config --global user.email "<[email protected]>"
git config --global user.name "your name"Add an SSH Key for more convenient authentication.
Generate a GPG Key for encrypted and signed commits.
git config --global user.signingkey "<your-GPG-key>"
git config --global commit.gpgsign trueLast updated
Was this helpful?