User:Stringo0/Git Cheat Sheet
Adding Files
When you create or change a file in the project folder, you can add it using:
git add FOLDER/FILENAME
If you want to add everything, do
git add .
New files must be added before they can be pushed to the server. Files that are modified need to be added as well, but the commit all command automatically adds modified files.
Committing
Whenever you make a change, you have to "commit" it before it can be pushed to the server. The commit command I use is:
git commit -m "Message for the commit" -a
This adds a message to the commit, and automatically adds all existing files that are modified (without needing to use the add command).
Pushing
Pushing pushes your commits to the github repository so that it can be downloaded/pulled by others. To push, simply type in:
git push
Pulling
Pulling pulls/downloads any changes made by others from the github repository. You should generally pull before working on files, and also make it a habit to pull before pushing. You can pull with the command "git pull".
Branches
Branches are used to develop a new feature, test something out, or making a major change in the code. Branches can later be merged into other branches once they are found to be working. For example, we are using the "dev" branch for development, while we are using the "master" branch for finished work.
To list all branches: git branch To Create a branch: git checkout -b branchname To Change to the new branch: git checkout branchname (Don't do this) To Just push the new branch to the server: git push origin branchname (Do this) To Push and connect your local branch to the server: git push --set-upstream origin branchname To Download a branch from the server: git checkout -b branchname origin/branchname
Merging a branch: First checkout the branch you want to merge the changes to. Then use:
git merge branchtomerge
Conflicts
Ocassionally, you might be faced with a conflict when you pull or merge a branch. A conflict arises when someone else has made changes to the file in the same location as you. When you get a conflict, the conflicting files will have markings in them like the following:
<<<<<<< yourbranch:README YOUR Changes and lines of code. ======= SERVER/OTHERS' Changes and lines of code. >>>>>>> branchname:README
Fix the conflict for each file by looking at these areas and changing these snippets to what the code should be in that area. Then run:
git commit -m "Fixing conflicts" -a
This commits the file and fixes the conflict with that file.
Other Important Commands
git status - displays the git status which shows modified files, new files, changes to commit etc.