Show Menu
Cheatography

Git Cheat Sheet (DRAFT) by

Get familiar with basic git commands

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Principles

Create a git repository for every new project
Create a new branch for every new feature
Branch early, and branch often
SSH keys are how we securely commun­icate btw our computer and GitLab

Typical Workflow

git clone <re­po>
clone repo located at <re­po> onto local machine, clone with SSH
cd my_project
after git clone, go to the directory, before using all the git commands
git checkout -b my_branch origin­/re­mot­e_b­ranch1
create and checkout my_branch, that is tracking remote­_br­anch1
git add <fi­le_­nam­e>
git commit -m "­mes­sag­e"
commit the change to my_branch
git pull
update local my_branch with remote commits and update all remote tracking branches
git merge origin­/re­mot­e_b­ranch2
merge remote­_br­anch2 with the branch that you are currently on (my_br­anch)
git push origin
push the branch that you are on (my_br­anch) to origin (remote repo)
(create a merge request on GitLab UI)
rmb to change target branch

Typical Workflow 2 - PyCharm

git pull
before pushing your changes, sync with the remote and make sure your local copy of the repository is up to date to avoid conflicts
git push
push changes from the current branch
define remote and select target branch
click on Define remote link (appears when there is no remotes in the reposi­tory), click on the branch name

Pushing and merging code change

git add <fi­le_­nam­e> / git add --all
add the file to the staging area
git commit -m "­sho­rt_­msg­"
takes a permanent snapshot of the current state of your repository that is associated with a unique identifier
git push origin <br­anc­h_n­ame>
pushes a local branch(es) to a remote repository (origin - the conven­tional shorthand name of the url for the remote reposi­tory)
git checkout main
checkout the default branch of your repo
git merge <br­anc­h_n­ame>
merge your branch into the default branch
git push
push the changes
 

Check

git status
list which files are staged, unstaged, and untracked
git branch
list all of the branches in your repo
-r to list the remote branches
-a to see all branches
git diff
show unstaged changes between your index and working directory
git log
list the version history for the current branch
git ls-files
check which files are in your staged area
git branch -vv
check tracking branches

Useful Commands

git init
creates a new git reposi­­tory, use this command while inside the project folder, this will create a .git folder
git branch <br­anc­h_n­ame>
create a branch
git checkout <br­anc­h_n­ame>
change to the branch
git branch -u origin­/re­mot­e_b­ranch
-u = --set-­ups­tream
set the tracking branch to be remote­_branch
git fetch
pulls in all the commits from your remote but doesn’t make any changes to your local files (will overwrite your current files)
git branch -m new_br­anc­h_name
rename a branch
git branch --delete branch­_name
delete a branch
git stash apply stash@{n}
restore a git stash, run git stash list to see the list
More info on setting upstream https:­//d­evc­onn­ect­ed.c­om­/ho­w-t­o-s­et-­ups­tre­am-­bra­nch­-on­-git/
Origin is the name which git gives to the remote repo that you cloned from

Making Changes

git reset --hard
to discard all local changes (new files created in the local Git workspace that have never been added to the index will remain in the project folder after the hard reset)
git reset <fi­le_­nam­e>
undo git add - remove staged version of the file
git reset HEAD~1
undo the prev commit
git revert HEAD
undo the prev commit (for remote branch)
git commit --amend -m "new messag­e"
edit commit msg

To be classified

git rebase <br­anc­h_n­ame>
copy our work from the current branch we are on to branch­_name
git checkout <br­anc­h_n­ame­>^
move up one commit of branch _name
git checkout HEAD^ (~4)
move upwards in a commit tree
git branch -f <br­anc­h_n­ame> <co­mmi­t_h­ash>
reassign a branch to a commit
git stash
takes your uncomm­itted changes (both staged and unstaged), saves them away for later use