For referencing the most used git commands, terminology, and workflows.
This guide is a work in progress and some of the content may be inaccurate.
If you find any issues or would like to contribue, please feel free to create a pull request.
This guide was written as a place to hold useful git commands in a centralized location to easily be referenced.
Git is a revision control system, a tool to manage your source code history. GitHub is a hosting service for Git repositories.
You can use Git alone for local version control. GitHub is primarily used for backup and collaboration.
Working Directory
This is where all of your files and directories that you're working on live. An example of this may be Desktop/my-app/
.
Staging Area
This is where you add files with $ git add
that are ready to be commited to the Git repository.
Repository
This is where all your snapshots/files are stored after you've commited them with $ git commit
.
- Initialize git
$ git init
. - Add files
$ git add <files_or_folders>
. - Commit changes
$ git commit -m "Useful message"
. - Create feature branch
$ git checkout -b <branch_name>
. - Repeat steps 2 and 3 until satisfied.
- Switch to Master branch and merge feature branch into it
$ git merge <branch_name>
. - Remove merged branch
$ git branch -d <branch_name>
. - Repeat.
$ git init
This will initialize git in the current directory and create a hidden folder called .git.
$ git status
This will display the git status. You'll use this a lot.
$ git log
This will display a log of commits made.
$ git add <file_or_folder_name>
Example
$ git add app.js
This will tell git to stage the file or folder specified.
$ git add .
Stages new files and modifications, without deletions
You'll use this one the most.
$ git add -A
Stages all changes including hidden files
$ git add -u
Stages modifications and deletions, without new files
$ git add *.<ext>
Example
$ git add *.html
This will add every file with the .html extension.
$ git reset HEAD <file_or_folder>
Example
$ git reset HEAD app.js
This is basically the opposite of git add
.
If you've begun making changes with the wrong branch checked out, you can stash your changes, then checkout the correct branch, and finally apply the stashed changes.
$ git stash
$ git checkout correct-branch
$ git stash apply
Create a file in the root directory called .gitignore.
Add file or folder names to the .gitignore file separated by line breaks.
.env
.passwords
node_modules
*.sublime-*
*.code-workspace*
$ git commit -m "A useful message."
This will commit and create a message for the files added to the staging area.
Checking out can loosely be thought of as, "I'm checking out/viewing the state of my project on this particular branch, or at this particular time."
$ git checkout <commit_hash>
Example
$ git checkout c7a2a019170031f89dadad42f49a2c38265756ac
This will checkout the commit with the matching hashtag.
Master is the central or main state of your application. After checking out a previous commit, you will likely want to switch back to the master to continue working.
$ git checkout master
$ git log
Find the hashtag that corrisponds to where you want to revert back to.
$ git revert --no-commit <short_or_full_commit_hash>
Example
$ git revert --no-commit c7a2a01..HEAD
This will revert you back to the selected commit. You'll want to perform a new commit with a message such as "Revert back to c7a2a01".
Branches are versions of code split off from the master branch, best used when testing features and development. Once changes are finalized, you can then merge the finished branch into the master branch.
$ git branch
Lists all branches
$ git checkout -b <branch_name>
This will create and switch to the named branch.
$ git checkout <branch_name>
Example
$ git checkout feature-fix
First checkout the branch you want to merge in to, typcially this would be master
$ git merge <branch_name>
This will merge the named branch into the branch currently checked out and create a new commit.
$ git branch -d <branch_name>
This will delete the named branch.
$ git remote -v
This will list remote branches.
$ git config --global user.name "John Doe"
$ git config --global user.email "[email protected]"
This will change your user name and email address globally.
$ git config user.name "John Doe"
$ git config user.email "[email protected]"
This will change your user name and email only for the repository/directory you're working in.
npm outdated
This will display a list of packages that are outdated. Packages in red means there's a newer version matching your requirements. Packages in yellow indicates that there's a newer version above your requirements, so proceed with caution.
$ git push
This will perform a basic push to the remote repository. If you're in a new local branch, follow the instructions output after performing a git push with the new branch checked out.
$ git fetch origin
$ git push -d <remote_name> <branch_name>
Example
$ git push -d origin feature-branch
This will push your changes to your Github remote repository and delete the named remote branch.
Delete a local branch