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 [email protected]:<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 .
NOTE: the previous two commands do not record removed files, to do so use the -A
option
$ git add -A .
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
$ 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
After create the pull request online you do:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ git merge --strategy-option theirs origin/<branch>
$ git push
How to merge Branch with Master
HINT: It is the same than above!
$ git checkout <branch>
$ git merge --strategy-option theirs origin/master
$ git push
NOTE: change the strategy to your convenience, see the man page.
How to remove a Branch
$ git branch -d <branch>
How to undo last commit
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 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
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 "[email protected]"
Some concepts
There is a difference between BRANCH
and origin/BRANCH
:
BRANCH
is the local one andorigin/BRANCH
is the remote one
Check out the full list of Git commands.