Frequently used Git commands
Git runs entirely on your workstation, and a copy of the entire repository is also on your local hard drive. GitHub, BitBucket, and other providers only give you a storage space to allow you to share your repository with others and provide a web user interface to manage it. You can use any provider’s application on your workstation to manage any of your Git repositories. Many developers use SourceTree, the great application written by Atlassian, the owner of BitBucket, to manage Git repositories on their workstations that are shared at GitHub.
Here are the most frequently used Git commands.
Create the local repository
Initialize a new Git repository in the current directory. This command creates the .git sub-directory to store your repository and its configuration file.
Display the local repository status
Display the list of added, deleted and modified files in the local repository
Display your changes in the files
Display the changes in files since the last git add
Display the changes in the local stage area after you have executed git add
git diff --staged
Stage your changes in the local repository
Add your changes to the local stage area
git add .
Save the changes to the repository
Commit your changes from the local stage area to the local repository with a message
git commit -m "My message"
Edit the last commit message
You can edit the last commit message even after the push.
git commit --amend
When you execute git status you get the message
Your branch and ‘origin/master’ have diverged,
and have 1 and 1 different commits each, respectively.
(use “git pull” to merge the remote branch into yours)
nothing to commit, working tree clean
To synchronize your local repository with the remote, execute
The merge window pops up. You can leave the default message, or type your explanation. To save your message and close the window
- Press the ESC key on your keyboard
- Press the keys (including the colon at the beginning) :wq
Push your changes to the remote
Send your changes to the remote repository
Push the changes to the remote repository at GitHub, Bitbucket, or others
Get the latest from the remote repository
Pull the latest changes from the remote repository
Push an existing repository from the command line
git remote add origin https://github.com/ORGANIZATION/REPOSITORY_NAME.git git push -u origin master
- Great article on a branching strategy that can serve small projects and large teams: A successful Git branching model
Forks and Pull Requests
- Good explanation of the recommended steps to fork an existing project: GitHub Standard Fork & Pull Request Workflow