Here are the most used and most common Git commands. This text assumes your primary branch is called main
.
Command | Effect |
---|---|
git status |
Will report the status of files changed on the local filesystem. Always check this status before messing around with checkouts, branches, pushes, pulls, etc. |
git checkout -b {branch} |
Create, and switch over to a new branch; this is how you make a new branch to work in. |
git checkout {branch} |
Goes to an existing branch; e.g., git checkout main . |
git branch |
See a list of branches. |
git merge {branch} |
Grab file changes from another branch and merge it into the current one; e.g., after git checkout main , you might do a git merge newfeature to merge the changes made in the newfeature branch back into the main branch. |
git branch -d {branch} |
Deletes the specified branch; don't worry, Git won't let you lose any work by deleting any branch. Useful for getting rid of temporary branches have had a merge into the permanent main branch for housekeeping. |
git commit -am {message} |
Takes a snapshot of your work, giving it a brief description (or "message") describing the changes. |
git push |
Pushes your local files over to "origin", the Github repository. |
git fetch |
Checks Github for changes; will not change files, it just lets you see if there's anything newer on Github that you need to pull . |
git pull |
Grab the latest code from "origin" on Github, back down to your computer. |
$ git status
(...make sure the working tree is clean...)$ git checkout -b newfeature
(...creates a new branch...)$ git commit -am 'Added a cool new feature'
(...creates a snapshot of your files after you make some changes; do this as often as you want, multiple snapshots can only be helpful if you have to go back to find problems...)$ git checkout main
(...switches the files in your working directory back over to the permanent "main" branch...)$ git merge newfeature
(...imports the changes you made in the temporary "newfeature" branch, to make them a part of the permanent "main" branch...)$ git branch -d newfeature
(...finally, a little housekeeping; deletes the now-unneeded temporary "newfeature" branch...)