in Code, Geeklife

My Git Cheat-sheet

I don’t know if it’s the crazy syntax, but for the life of me, I always need to come back to this cheat sheet, maybe you will too:

GIT CHEATSHEET

fetch remote branch.
git fetch origin nameofbranch
“fetch” downloads the changes of the remote branch but doesn’t automatically merge them.
If you have commited local changes on that branch, you usually fetch and then rebase your changes at the end of the fetched updates.

push local branch to remote
git push origin (pushes current branch to default remote branch)
git push origin nameofbranch (pushes current branch to the remote nameofbranch branch)

delete remote branch (who the fuck thought of this syntax?)
git push origin :branchToDelete (deletes branch on remote repo)
git branch -D branchToDelete (deletes branch on local repo)

clean untracked files (not folders)
git clean -f -x

list your remote repository aliases and full paths
git remote -v

How to undo the last commit but keeping the changes
git reset HEAD~1

How to remove the last commit from the remote
git reset --hard HEAD~1
git push --force (careful with the –force if you’re working with somebody else)

While working on a feature branch

“Ideally, you do git pull –rebase or git rebase upstream/master instead of merges while working on a feature branch so that you don’t get merges.”
-@laanwj

Discard local changes and replace with what’s on the remote repository for current branch.
git fetch origin
git reset --hard origin/myBranchHere

Rebase+Squash all changes of your feature branch (since you started that branch out of master.)

Standing on the branch…

git rebase -i master (the -i stands for interactive rebase)

All commits will shown.

Switch to Pull Request branch
git fetch origin pull/ID/head:BRANCHNAME
$ git checkout BRANCHNAME

Checking out a branch from someone’s fork of your project to test/fix pull request.
Say user ‘alice’ is submitting a pull request from a branch called ‘wonderland-feature’
You want to test that branch locally, this is how you get it.
#first add a remote for that repo.
git remote add alice https://github.com/alice/our_project
#download her branch
git fetch alice wonderland-feature
#create a local branch with her remote wonderland-feature branch
git checkout -b wonderland-feature alice/wonderland-feature

Now git log should show the same commits as on her repo.
Commit to the branch if you want to fix something for alice, and then push to your
fork (by convention your fork is called origin) of the repo, then send her a pull request
from your fork.
# uploads the branch to your fork, from there send her pull request.
git push origin wonderland-feature

Once she has merged your changes, if you feel her branch is good now, you can now merge hell pull request on the upstream repository.

Write a Comment

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.