Skip to content

Instantly share code, notes, and snippets.

@llkats
Last active November 26, 2023 06:38
Show Gist options
  • Save llkats/1886918 to your computer and use it in GitHub Desktop.
Save llkats/1886918 to your computer and use it in GitHub Desktop.
git cheatsheet

git flow feature start <branchname>
starts a new branch

git commit -am "commit message"
make commits as usual

git pull
from master branch, pull in latest changes from the repo (repeat often)

git pull origin <branchname>
while on , pull in latest changes for that branch

git checkout <branchname>
checkout a new branch that already exists

git push origin <branchname>
push a single branch up to the repo for testing

git pull  
git merge —no-ff <branchname>  
git push```  
steps to merge a branch into master and push it to the repo

```git stash  
git checkout <correct branchname>  
git stash pop```  
steps to move changes accidentally made on the wrong branch to the correct one

```git commit --amend```  
edit the commit message you just made (not having pushed it)

```git checkout -- <filename>```  
forget all the changes you made to a specific file and revert to its state beforehand, before you commit

```git reset --hard HEAD```  
forget all the changes you make to all uncommitted files and revert to their states beforehand

```git checkout master  
git pull  
git checkout <branchname>  
git merge master```  
steps to merge master into a branch. once any conflicts are resolved, you can merge the branch into master

```git checkout master  
git merge <branchname>  
git push```  
steps to merge a branch into master

```git push origin —set-upstream feature/branchname```  
push a branch to master, then make a pull request on the github

```git flow feature finish <branchname>```  
finishes a branch, merging and deleting it locally, and updates its status in Redmine to ‘QA and Close’

```git branch -d <branchname>```  
deletes the branch. use -D flag if it throws errors.

```git push origin :<branchname>```  
deletes the branch from GitHub

```git branch -m old_branch new_branch```  
rename a branch

```git log -p```  
show the log with all accompanying diffs

```git log --oneline```  
show the log with one line of accompanying commit messages

```git stash show -p | git apply && git stash drop```  
creates a patch, pipes that to the apply command and if that succeeds without conflicts it drops the just applied stash item
via: http://stackoverflow.com/questions/1360712/git-stash-cannot-apply-to-a-dirty-working-tree-please-stage-your-changes

```git config --global credential.helper cache```  
stores login credientials for 15 minutes

```git add file_name1  
git commit --amend -C HEAD```  
add a forgotten file to an existing commit

```git add -p```  
add only parts of files to a commit

## rebasing to squash commits
combine edits into one commit to make a neater pull request

```git rebase -i HEAD~2```  
rebase the last **2** commits
* goes to an interactive screen where you apply commands to each commit
  * on the second commit hash, change `pick` to `squash` or `fixup`
  * `squash` retains the commit messages, while `fixup` discards the second message
* after saving, there's a second screen with the commit messages (I guess you can edit them there)
* easiest to rebase after each commit so you're only rebasing two commit messages at a time and don't have a lot to squish at a time

to sync your forked branch with a remote repo master
1. `git pull --rebase upstream master`
  * `upstream` is the main repo (not the fork)
2. make edits
3. `git add whatever-conflicted-file.js`
4. edit all of the conflicted files
5. `git rebase --continue`

@dideler
Copy link

dideler commented Feb 22, 2012

Hey, I noticed your gist via the All (Public) Gists page.

You should check out http://help.github.com/git-cheat-sheets/ if you haven't already!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment