Skip to content

Instantly share code, notes, and snippets.

@TheFern2
Last active September 25, 2019 23:34
Show Gist options
  • Save TheFern2/bdb1e018791c70507ebbd7025f0a5cd2 to your computer and use it in GitHub Desktop.
Save TheFern2/bdb1e018791c70507ebbd7025f0a5cd2 to your computer and use it in GitHub Desktop.
Git Commands for mere mortals

Better commit messages

This is my custom commit message:

# If this commit is applied, it will... (Less than 50 chars)

# Why was this change made?

# How does it address the issue?

# Any references to tickets, articles, etc?

To activate on Windows:

git config --global commit.template %HOME%/.git_commit_msg.txt

or Linux or Mac:

git config --global commit.template ~/.git_commit_msg.txt

Then you can do:

git commit -a

or simply

git commit

And of course you can still do:

git commit -m "init"

Change Default Editor

git config --global core.editor "code -w"

To see default configuration:

git config core.editor

Git Most Used Commands

Init a repo locally

touch README.md
git init
git add *
git commit -m "init repo"

Push a local repo to github or remote server

git remote add origin https://github.com/user/repo.git
git push -u origin master

Replace Github address if using another server. i.e. 174.168.1.23:3000

Create a Repo on github with README.md

On github, create a new repository, and make sure to add README.md, can also add license and .gitignore though is not necessary.

Clone Locally

Once the repo is created on github, then clone locally, and you can proceed to work on the repo.

git clone https://github.com/user/repo.git

Working with Branches

If your project is small enough, chances are you are doing everything on the master branch. If you have a medium size project, or just working on a dev feature you can add a branch:

git checkout -b "feature-new-stuff"

Make changes on branch

git add *
git commit -m "new stuff"
git push origin "feature-new-stuff"

Option to do all branches to remote:

git push --all

Merging branch to master

git checkout master
git pull origin master
git merge "feature-new-stuff"
git push

Working with Forks

When working with forks create a new branch and do your work there, you have to ensure you are even with the upstream master, otherwise you can potentially work on something and run into merging issues later on.

Show remotes:

git remote

Output:
	origin
	upstream

If for whatever reason you don't have an upstream remote, is easy to add one:

git remote add upstream https://github.com/user/repo.git

Then fetch changes from master repo:

git fetch upstream

Lastly merge changes:

git merge upstream/master
git push

Tags

Docs Tags

Lightweight tags, locallly

git tag v1.0.0
git push origin v1.0.0

Tags upstream

git push upstream v1.0.0

Never do --tags!

Local branch

git push origin branch-name
git pull origin branch-name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment