Git cheatsheet
Frequently used commands
Creating a repository
- Create a local Git repository
- To obtain a remote repository. Get the
remote_urlfrom GitHub
Local changes & commits
- View the status of your repository
- Add a file to the staging area
- Remove a file from the staging area
- Commit everything in the staging area. Choose a concise and descriptive message.
- Show the commit history. Press
qwhen reaching the end of the log to exit.
- Show the commit history. Uses a single line per commit. Less detail, easier to keep the overview.
Branching
- Check what branch you are currently on
- Switch to a different branch
- Create a new branch from the current branch
- Merge all the changes on
other_branchinto the current branch
- Delete a (local) branch
Syncing with remote
- Show information on the rconfigured remote(s)
- Obtain changes from remote. This does not change your code locally.
- Obtain the changes to the current branch from remote. Merge them with your local branch.
- Upload your local changes to the current branch to remote. Merge them with the remote branch.
Undoing changes
- Remove the commit with
commit_hash. Keep the changes made in that commit in the staging area. Discards history for the commit.
- Create a new commit that undos all changes in
commit_hash. Preserves history for the commit.
Workflow example
In this example, we will add a new function that calculates square roots to our imaginary script mathematics.py. A
- Start on your local
mainbranch git pullto ensure you are up-to-date with remote- git branch
feature/sqrt - Write the code in
mathematics.py git add mathematics.pyto stage the changesgit commit -m "add square root function"- (optional) Obtain feedback from a peer
git pushto upload your local branch to remote. You may need to set the upstream (remote) branch. Git will provide you with the command.- Create a Pull Request on GitHub and ask your peer for feedback.
- If the PR is approved, continue with step 8.
git checkout mainto return to the main branchgit merge feature/sqrtto merge the changes into maingit pushto make sure the changes are also on remote- (optional)
git branch -d feature/sqrtto clean-up the feature branch