Assuming our repository has already been set up with git init
and git remote add origin
, this is the general flow one might follow to keep a clean development space. If you need to create a new project, hit New Repository from GitHub and it will walk you through the steps to get going.
Once we're ready to develop, we always want to make sure we have the latest changes from master
.
git checkout master
git pull
Now, we need to do our work. We first should check out our feature branch
git checkout featureBranch
or create the branch if it doesn't exist yet (ie. we're just starting!)
git checkout -b featureBranch
# It's a good idea to go ahead and push up a new branch so the origin knows about it.
git push -u origin featureBranch
Now, we make our changes. Once me make our changes, we need to add them to Staging.
git add filename.txt
# or
git add directory/
# or
git add -a
git add -a
will add all of the changes from your tracked files to staging. Note that this means if you have created a new file, and haven't added it yet, this command will not add it because it isn't tracked by git yet.
Once we are ready to commit the changes we have staged, we run
git commit -m "Description of commit"
And finally we push our changes up.
git push
These are the general steps you will take when working. However, remember that other people are likely working on your project too!
As often as you see fit (usually, at minimum, right before and right after you write new code) you should merge master
into your branch.
# swap to master
git checkout master
# pull any changes
git pull
# go back to our branch
git checkout myBranch
# merge master's changes into our own branch
git merge master
# push the result to remote
git push
The more often your merge master into your branch, the less likely it is your changes will cause merge conflicts.
Once you finish working and are ready to merge back into master, you should create a Pull Request from GitHub and have another developer review it (preferrably someone who didn't work with you on the code).
If you don't want to use Pull Requests, you can also merge into master using the reverse of the above commands.
Once the branch has been merged in, it's a good idea to delete it from both GitHub and your local.
git branch -d branchName
Often, you might start work and get some decent work done before you remember you never pulled down the remote changes. If you try to run a git pull
while you have unstaged/uncommitted changes, git will often yell at you. This is the situation in which we use git stash
.
# Starting our on feature branch,
# this will stash all of our changes away.
git stash
# Now, we pull the remote changes into our branch
git pull
# Finally, we put our changes back
git stash pop
If you try to merge a branch into your own or try to pull down changes after you've made some of your own, you may run into conflicts. Conflicts are git's way of saying "Hey, multiple people have changed the same lines of code and I don't know which one is right."
The best way to deal with conflicts is to use a tool that helps you resolve them. IntelliJ, WebStorm, and Android Studio all have fantastic tools for this, but many other IDEs do, too.
After you have resolved your file by getting it to look the way you want, you need to make sure it has been added back to Staging.
git add fileThatHadConflicts
A normal merge creates a merge commit when it succeeds. However, we have not created a merge commit yet, since we had conflicts; we need to do that now.
git commit -m "Merging"
Finally, don't forget to push the merged changes!
git push
When creating a new repository
git init
To see what changes are untracked/unstaged/staged
git status
To add a remote repository
git remote add origin https://github.com/yourname/repo-name.git
To add changes into "Staging" (aka Staging Changes)
git add filename.txt
# or
git add directory/
To commit your changes
git commit -m "Description of commit"
To push your changes
git push
# or, if you've never pushed your current branch before,
git push --set-upstream origin branch-name
To see all branches, including remote branches
git branch -a
To create a new branch off of the current branch you are on
git checkout -b newBranchName
To swap to a branch that already exists
git checkout branchBame
To merge changes from anotherBranch
into your current branch
git merge anotherBranch
To delete a local branch
git branch -d branchName