Regular Coding Workflow Link to heading
# Sunny day scenario
git clone https://github.com/torvalds/linux.git # create local clone of a remote git repo
git pull # pull latest from remote. This is not needed if you just cloned
git checkout sprint_branch # checkout the branch you want to create story branch from
git checkout -b story-1 # create a new story branch off of sprint_branch for all your code changes
# make all the code changes/test. Basically, change the world here
git diff # Check what you changed BEFORE committing
git add newfile1.py newfile2.py # add any new files your created
git commit file1.py file2.py newfile1.py newfile2.py # commit changes to story branch
git push --set-upstream origin story-1 # push your branch/changes to remote
# If the sprint_branch has had commits since we created story-1 we need to merge such
# commits from sprint_branch to story-1
git checkout sprint_branch
git pull
git checkout story-1
git merge sprint_branch
# Now you can create a PR on say github UI
Find Latest Commits and Their diffs Link to heading
git checkout story-1
git log # shows an overview of commits in reverse chronological order. Keep hitting space bar to scroll down
git log -p # shows the actual code diff
Dealing with Uncommittable Code Link to heading
Suppose you are in the middle of code changes but you don’t want to commit it just yet. In this case we can stash (not commit) all the code with a save message
git stash save 'stash message'
# Life happens e.g. extinguish production fires here
# Once you want to come back to the uncommited code
git checkout story_1
git stash list # this lists all the stashes you have with their save message, if any.
git stash apply "stash@{n}" # replace n with the actual number from above output
git stash clear # remove all stashes
Miscellaneous Commands Link to heading
# revert a previous commit using its commit hash. This is better than trying to manually
# reverting a change which is prone to human error.
git revert <commit_hash>
# same note as above. Let git do the heavy lifting
git cherry-pick <commit_hash> # cherry pick a commit from another branch to this branch
git blame file1.py # find the latest author and related metadata of each line in a file
git gc # does gargabe collection to save disk space and improve git performance