Typical Git Commands
This guide will help you as you get started with git. It doesn’t cover all the details, but it also doesn’t surround you with detail you won’t use.
Typical Workflow
- Get all code (clone)
- Switch to a starting branch (checkout)
- Create your own branch (checkout)
- Add new files / Make changes to files
- Stage all your changes (add)
- Commit your changes (commit)
- Push your changes
Sometimes you will repeat steps 4,5,6 several times. In which case you might want to squash your changes (will be explained later)
Commands
git clone <repo url>
cd repo name
git checkout develop
git checkout -b branch-name
git add mynewfile.js
git add existingfile.js
git commit -m "Added new file, modified exssitng file"
git push origin branch-name branch-name
Detailed Explanations of the above
-
git clone <repo url>
This command will download the repository and it’s history on to your local machine.
Sample:git clone git@github.com:SameerDoshi/gittutorial.git
-
cd repo name
This simply moves to the directory that was created Sample: cd gittutorial -
git checkout develop
Typically you will not be allowed to make changes directly to the master branch. But instead will have to make changes to the develop branch. Others may have changes in develop that are not yet in master. You should start with develop. This command changes your branch to develop. Sample: git checkout develop -
git checkout -b branch-name
Now you’ll create a custom branch name that will track all of your work. Branch names are case sensitve and I reccomend you do not use spaces. Sample: git checkout -b adding-index
5.git add mynewfile.js
After creating (or modifying) files you need to add them to the staging area. To do this use the add command.
Sample: git add index.html
-
git commit -m "Added new file, modified exssitng file"
When you’re finished adding all your files you’ll commit and include a commit message. If you don’t use-m "a sample message"
then you’ll be promotped to enter a message. Sample: git commit -m “Initial commit” -
`git push origin branch This command pushes your branch to the server Sample: git push origin adding-index
Additional Help
The GitHub HelloWrold project provides details for how the entire workflow above.