Git basics and common commands. Your one-stop-shop to all things Git!
Creates a new branch. It's recommended to create a new branch per feature. When working in groups, it's helpful to add your initials to the branch name. This helps teammates quickly identify who is working on what features.
# git checkout -b <new_branch>
git checkout -b submit-btn
#git checkout -b <new_branch -user_initial>
git checkout -b new-user-form-LN
Returns the current branch you are working on.
# git branch to see current branch
git branch
Switches from current branch to existing branch. Be sure to save changes in current branch before switching!
# Double check which branch you are on
git branch
# git checkout <branch_name> to switch between existing branches
git checkout new-user-form-LN
Unstages local files that were staged in git add.
# Unstages specific file
git reset script.py
# Unstages all files
git reset
# throws away all your uncommitted changes
git reset --hard HEAD
# Double check which branch you are on
git branch
# git checkout <branch_name> to switch to main branch
git checkout deployment
# git merge feature branch into main branch
git merge new-user-form-LN
Once feature branches are merged and you have confirmed they are not needed, you can delete it. Though, many teams keep all branches for records.
# Double check you're not in the branch you want to delete
git branch
#### git branch -d <branch_name>
git branch -d new-user-form-LN
# Git add all files with the same extension
git add '*txt'
# Git remove all files with the same extension
git rm '*txt'
Shows history of commits.
# git log to see history of commits
git log
Adds a remote repository to a git folder.
# Git add remote repo
git remote add origin https://github.com/try-git/try_git.git
Removes remote repo from Git folder. Please check with your lead before running this command.
# Git remove remote repo
git remote rm origin https://github.com/try-git/try_git.git
Pushes master branch to remote repo. Though, if you created a deployment branch to test merge conflicts, the command will be:
# Git push deployment branch
git push origin deployment
Pulls down recent changes to your local repo. Though, if you created a deployment branch to test merge conflicts, the command will be:
# Git pull deployment branch
git pull origin deployment
Shows differences the most recent commits.
#Shows differences between most recent commits.
git diff HEAD
Shows differences the most recent commits in staged files.
#Shows differences between most recent commits of staged files.
git diff --staged
Resets file to last commit.
git checkout -- myfile.py
press "i"
write your merge message
press "esc"
write ":wq"
then press enter
- When adding a commit message, it's helpful to capitalize file names.
git commit -m "created README.md file"
- Pressing the arrow up key will display previous inputs. This is helpful when you're running the same commands repeatedly.
- When typing in a file name, you can press TAB after a few letters, the terminal will guess the file name. If there are multiple names, terminal will display all files that match the first couple of letters you've typed. File names are case sensitive.
Creating aliases will save you time by allowing you to type shortcuts.
#turns
git checkout
#to
git co
#turns
git branch
#to
git br
#turns
git commit -m "Your commit message"
#to
git ci -m "Your commit message"
#turns
git status
#to
git st
#turns
git add <file_name>
#to
git a <file_name>
#git last to show last commit
git last
commit 66bb74542905b760f8b319b00739800d5ecaaef1
Author: Lisa <[email protected]>
Date: Tue Jun 27 09:43:30 2017 -0700
- Group chat
- Starting a group chat is a great way to keep everyone on the same page. It's great place to announce what features you're taking next and to ask for help. I like to announce when I've pushed to deployment so my teammates can pull the latest changes.
- Pull before push!
- Though you should pull changes from the remote repo before making changes on your local repo, it's possible a teammate has pushed changes to the remote repo while you were working. It's best practice to pull all changes from the remote repo before pushing up changes from your local repo. Remember to tell your team you've pushed!
- NEVER PUSH TO MASTER
- Create a new deployment branch before making any changes. Use this deployment branch to check merge conflicts. Once all branches are merged into the deployment branch and all is fine, THEN merge the deployment branch into the master branch.
If you'd like to go into more depth, try Git Real with Code School or Codecademy's tutorial. It will take a few hours.