The below are some notes on Git which includes settings up and using.
Install
Set your identity for your global Git settings
SSH keys
User guide
Create a new repository
Work on develop branch
Before pushing changes
Merge from develop into master branch
Delete unwanted branches
Common git commands
$ brew install git
Once git is installed there are some additional steps for your machine.
One of the first things you should do after installing Git is set your user name and email address — this is important because every Git commit uses this information. From the command line, run the following to set your user name and email globally:
$ git config --global user.name "John Doe"
$ git config --global user.email [email protected]
The next important thing to understand when using Git regularly is SSH keys.
So what is an SSH key? An SSH key serves as a means of identifying yourself to an SSH server, using something called public-key authentication. What this means is instead of having to type in a username and password each time we want to access a server (ie. GitHub), we can authenticate using a special key-pair. Essentially, what we’re doing is creating a way to login to a specific server, without using a password because that server will be able to identify your computer.
Let’s set up an SSH key with GitHub so there’s no need to type in our username and password each time we want to push changes we make locally.
Generating an RSA key pair provides you with two long strings of characters: something called a public and a private key. What we then do is place the public key on any server (in this case it will be GitHub), and then unlock it by connecting to it with a client (our computer) that already has the private key.
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
Where the 4096 refers to a 4096 algorithm, which is recommended by GitHub when setting up SSH keys. Make sure to replace [email protected] with the email address you used to sign-up for GitHub, and that it matches the email you set Git to use globally on your computer.
Once you’ve typed in the previous command, you’ll be asked where you want to store the keys.
$ Enter file in which to save the key (/Users/username/.ssh/id_rsa):
Next, you’ll be asked if you want to set a passphrase. For extra security you can use a passphrase, but I would say most people don’t bother to enter one, just because you’ll need to type it every time you use your key pair. To skip entering a passphrase, just hit return on your keyboard when it prompts you for one.
$ Enter passphrase (empty for no passphrase):
Your public and private key will live wherever you’ve chosen to save them on your computer. Usually this is in a hidden .ssh
folder as id_rsa.pub
for your public key, and id_rsa
for your private one.
From your terminal, you should be able to type the following to copy your public SSH key to the clipboard:
$ pbcopy < ~/.ssh/id_rsa.pub
The ~
should take you to the root folder of your user (when I generated my SSH key on a Mac, you saw it was saved inside Users/username
— the ~ brings me to the same directory).
Then go to GitHub, and click on your profile photo > Settings > SSH and GPG keys > New SSH key / Add SSH key.
In the Title field, add a description that will help you identify which machine the new key is for. For example, if you're using a work Macbook Pro, you might call this key "Work MacBook Pro."
Then, paste your key into the Key field using cmd
v on a Mac or ctrl v
on a PC, and click Add SSH key. If you’re prompted to, confirm your GitHub password and you’re all set!
Now you should be able to push commits from your local machine to GitHub, without having to type your GitHub username and GitHub password every time! Just make sure to start a new terminal or GitBash session for it to take effect.
-
Create a new repository on GitHub. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.
-
Open Terminal.
-
Change the current working directory to your local project.
-
Initialize the local directory as a Git repository.
$ git init
-
Add files into the new local repository. The following command stages the new files for the first commit.
$ git add . # Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
-
Commit the files that you've staged in your local repository.
$ git commit -m "First commit" # Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
-
In Terminal, add the URL for the remote repository where your local repository will be pushed.
$ git remote add origin remote repository URL # Sets the new remote $ git remote -v # Verifies the new remote URL
-
Push the changes in your local repository to GitHub.
$ git push -u origin master # Pushes the changes in your local repository up to the remote repository you specified as the origin
-
Before creating a new branch, pull the changes from upstream. Your master needs to be up to date.
$ git pull
-
Create the branch on your local machine and switch in this branch:
$ git checkout -b <:name_of_your_new_branch:>
-
Push the branch on github:
$ git push origin <:name_of_your_new_branch:>
Before starting any work, make sure you are on the Develop
branch.
$ git status
- Record changes in CHANGELOG.md
- Push to Git using the appropriate method outlined below
On your development repo, ensure that all changes in develop
have been committed and pushed.
Merge develop
into your local master
branch and tag with the version number:
$ git checkout master
$ git pull -v
$ git merge --no-ff --no-edit develop
$ git tag -a v0.0.0 -m "Release 0.0.0"
# Now push these changes to Github
$ git push
$ git push origin v0.0.0
$ git branch -a
# List branches in local machine
$ git checkout <:branch-you-wish-to-move-to:>
# Move to a new branch
$ git branch -d <:branch-you-wish-to-delete:>
# Delete the old branch
$ git remote prune origin
# Cleanup the local references
$ git init
Utility: To initialise a git repository for a new or existing project.
How to : git init
in the root of your project directory.
$ git clone
Utility: To copy a git repository from remote source, also sets the remote to original source so that you can pull again.
How to: git clone <:clone git url:>
$ git status
Utility: To check the status of files you’ve changed in your working directory, i.e, what all has changed since your last commit.
How to: git status
in your working directory. lists out all the files that have been changed.
$ git add
Utility: adds changes to stage/index in your working directory.
How to: git add .
$ git commit
Utility: commits your changes and sets it to new commit object for your remote.
How to: git commit -m ”sweet little commit message”
$ git push/git pull
Utility: Push or Pull your changes to remote. If you have added and committed your changes and you want to push them. Or if your remote has updated and you want those latest changes.
How to: git pull <:remote:> <:branch:>
and git push <:remote:> <:branch:>
$ git branch
Utility: Lists out all the branches.
How to: git branch or git branch -a
to list all the remote branches as well.
$ git checkout
Utility: Switch to different branches
How to: git checkout <:branch:> or **_git checkout -b <:branch:>
if you want to create and switch to a new branch.
$ git stash
Utility: Save changes that you don’t want to commit immediately.
How to: git stash
in your working directory. git stash
apply if you want to bring your saved changes back.
$ git merge
Utility: Merge two branches you were working on.
How to: Switch to branch you want to merge everything in. git merge <:branch_you_want_to_merge:>
$ git reset
Utility: You know when you commit changes that are not complete, this sets your index to the latest commit that you want to work on with.
How to: git reset <:mode:> <:COMMIT:>
$ git remote
Utility: To check what remote/source you have or add a new remote.
How to: git remote
to check and list. And git remote add <:remote_url:>