Skip to content

Instantly share code, notes, and snippets.

@mamedinfo
Forked from jednano/gitcom.md
Created March 16, 2021 03:19
Show Gist options
  • Save mamedinfo/ddf6ed1036d13df1ae42fc47035054e8 to your computer and use it in GitHub Desktop.
Save mamedinfo/ddf6ed1036d13df1ae42fc47035054e8 to your computer and use it in GitHub Desktop.
Common git commands in a day-to-day workflow

Git Cheat Sheet

Initial Setup

Create an empty git repo or reinitialize an existing one

$ git init

Click the "Fork" button at the top-right of any repository's GitHub page.

Clone the codepainter repo into a new directory called codepainter:

$ git clone https://github.com/jedhunsaker/codepainter.git codepainter
$ git remote -v
$ git remote add upstream https://github.com/username/codepainter.git
$ git fetch upstream

Every-day Commands

When working on a fork, you could be switching between different branches quite commonly. As such, you generally want to stay off the master branch and work on your own feature branches so that master is always clean and you can base new branches off of it.

$ git checkout -b <new_branch_name>

If upstream has a special develop branch or something, you can checkout that branch separately, but setup tracking so you can sync it up from time to time. Like the master branch, don't work directly on this one. Try to keep it clean.

$ git checkout -b <new_branch_name> --track upstream/<remote_branch_to_track>

Maybe you made some progress on a branch at work, but now you want to continue work at home. In that case, you're dealing with your own fork's branch, so you'll checkout from origin.

$ git checkout -b <new_branch_name> --track origin/<remote_branch_to_track>

Use the -B option flag to force it.

Now, you can easily switch between branches with git checkout.

$ git checkout master
$ git checkout develop
$ git checkout feature_x
$ git config --global alias.co 'checkout'
$ git co master

Not sure if you're working on a clean branch? Want to see what files have changed? Git status will show you a report.

$ git status

Now that you've added or modified some files, you need to stage those commits into "the staging area." Think of git commits like an array of airlock hatches on a space ship. On this space ship, you can only open the door to one airlock at a time. When you open the hatch, you can put stuff in or take stuff out at will. Not until you've closed the door have you committed those changes (git commit) and not until you hit the red button do all those hatches open up into space (git push).

You can stage inidividual files or all files at once.

$ git add foo.js
$ git add .

Maybe you accidentally staged some files that you don't want to commit.

$ git reset HEAD foo.js
$ git reset HEAD .
$ git config --global alias.unstage 'reset HEAD'
$ git unstage .

Commit often. You can always squash down your commits before a push.

$ git commit -m "Fixed IE issues"

Want to automatically stage files that have been modified and deleted, but new files you haven't told git about will be unaffected? Pass the -a or --all option flag:

$ git commit -am "Fixed IE issues"

Maybe you have 4 commits, but you haven't pushed anything yet and you want to put everything into one commit so your boss doesn't have to read a bunch of garbage during code review.

$ git rebase -i HEAD~4

An interactive text file is displayed. You'll see the word "pick" to the left of each commit. Leave the one at the top alone and replace all the others with "s" for squash, save and close the file. This will display another interactive window where you can update your commit messages into one new commit message. I like to use "f" instead of "s", because I usually work in such a way that I name my first commit appropriately from the get-go. "f" just skips the 2nd interactive file and uses the first commit message.

Push a local branch for the first time:

$ git push --set-upstream origin <branch>
$ git push

Push a local branch to a different remote branch:

$ git push origin <local_branch>:<remote_branch>

Use the -f option flag to force it.

Manually set tracking.

git config branch.<local_branch>.remote origin
git config branch.<local_branch>.merge refs/heads/<remote_branch>

Do a pull, stacking your recent commits on TOP of the pulled commits.

git pull --rebase

Undo last push

git reset --hard HEAD~1 && git push -f origin master

Rebase on upstream master

git fetch upstream && git rebase upstream/master

List the set of repositories ("remotes") whose branches you track.

git remote -v

Checkout as CRLF, Commit as LF

git config --global core.autocrlf true



### [Deleting Branches]()

Delete a local branch:
```shell
$ git branch -d <local_branch>

Use the -D option flag to force it.

Delete a remote branch on origin:

$ git push origin :<remote_branch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment