Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git for humans

How to get a remote repository (from BitBucket, GitHub or anyone)

$ git clone https://github.com/<username>/<repository>.git

If you have added a SSH key, then you can also use this command:

$ git clone git@bitbucket.org:<username>/<repository>.git

How to create a new repository from the command line

$ touch README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git remote add origin https://github.com/<username>/<repository>.git
$ git push -u origin master

How to updates files in GitHub repository when it has been already created

First, get the repository and open it..

$ git clone <url:repository> # See above
$ cd <repository>

You can specify the file(s) and/or folder(s) to update

$ git add [<file|folder>[<file|folder>[...]]]

Or you can add the whole directory

$ git add .

Now commit the changes (record them locally):

$ git commit -m "my commit message"

After one or many commits you can use push to upload them into the remote repository

$ git push

NOTE: the first time you will need to enter:

$ git push -u origin master

I'm getting errors

If a commit was added from the Web but not in the local directory, then will be an error trying to add something from a terminal.

To solve this you can:

  • Make a copy of your current files
  • Download the most up-to-date repository (use clone)
  • Update the downloaded repository with your changes
  • And now add, commit, etc..

How to close a bug while committing on GitHub

$ git commit -m "Close #BUG_ID after implement security"

How to change a commit message

If a commit was made and not yet published (previous git push), then it's simple to change its message:

$ git commit --amend -m "A new message for the previous commit"

How to create a branch

$ git checkout -b <newbranch> # Generate and checkout new branch

How to merge Master with Branch

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ git fetch
$ git merge origin/<branch>
$ git push

NOTE: before this you may or may not have created a pull request (PR) online, this PR is a concept that applies to GitHub only.

How to merge Branch with Master

HINT: It is the same than above!

$ git checkout <branch>
$ git fetch
$ git merge origin/master
$ git push

NOTE: change the strategy to your convenience, see the man page.


How to remove a Branch

Thanks to Matthew Rankin!

Locally:

$ git branch -d <branch>

Externally:

$ git push origin --delete <branch>

How to undo last commit

Thanks to Andrew!

If you do not want to lose any change:

$ git reset --soft HEAD^ # Like you never did git commit

After that, remember to do git reset HEAD . to unstage files (it will not destroy any change!).

If you also want to destroy your changes:

$ git reset --hard HEAD^ # Like you were lazy after second last commit

Pay attention with the times that you apply git reset! If you apply it twice, it will revert also the second last commit. You could end up having a diverged branch, then you'll need to do git pull, and conflicts aren't fun ;)


How to undo last merge

Thanks to Marcin Gil!

$ git reset --hard HEAD~1 # Merge needs to be the last thing done

How to go back in history

First thing is to know where to go, find the hash of the commit to go:

$ git log
commit 1234567890abcdef1234567890abcdef00000000
Author: Name <email>
...

Then deatach the HEAD to the commit wanted:

$ git checkout 123456789 # Using the first nine is more than enough

Thanks to Jefromi!


Generate tags

NOTE: before do it you will like to save any local change (commit & push).

$ git tag -a 1.1 -m "my commit message"
$ git push --tags

Remove tags

This will only remove it locally. Otherwise you may want to take a look at the bottom of this.

$ git tag -d 1.1

User's Configuration

Check your actual configuration with the following commands.

$ git config --get user.name
$ git config --get user.email

Add and/or update the values with the following commands.

$ git config --global user.name "Your name"
$ git config --global user.email "example@server.dot"

Some concepts

There is a difference between BRANCH and origin/BRANCH:

  • BRANCH is the local one and
  • origin/BRANCH is the remote one

Check out the full list of Git commands.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Oct 9, 2017

Is enough to use the previous commands in order to create a new repo?
Cause, from command line, I had some trouble and I solved with this resource

Please, check it.
Anyway, thanks for this useful summary!

@luciomartinez

This comment has been minimized.

Copy link
Owner Author

@luciomartinez luciomartinez commented Feb 20, 2018

Hey sorry for the old reply, I just see it.

Yes, it should be enough. Anyways your source is really good though it looks a bit more complex.

Thanks!

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