For those who already have experience with git the next step tends to be contributing to a code base multiple people are working on. There are two ways to contribute, have access to the repository or fork the repository and do a pull request with the commits you want included in the main repository.
Replace everything in red with your respective names
$ git clone https://github.com/github-group/repo-name.git
Gaining access to the repository
If the repo is in a group then you need group access from the group administrator then you need write permissions for the repository from the repository owner to start committing to it. Then you need to set your git remote origin to point to that specific repository
Pull requests
Pull requests are a useful way to share code of an existing repository you don’t have access to or from a branch that needs to be merged into the master branch
Adding features
When working on a code repository it’s important to do all new features on feature branches, preferably with the branch named after the feature being implemented.
$ git checkout -b branchname
$ git push origin branchname
When you want to switch to an existing branch:
$ git checkout branchname
But it’s also important to keep the branch updated with any changes on the master. The rebasing option puts all your recent commits on top of the pulled commits.
$ git stash
$ git checkout branchname
$ git pull --rebase origin master
$ git push
$ git stash pop
Commiting
In order to save your work you need to commit it. But make sure you only save the specific changes you want in your commit and nothing else. To do that it’s important to do git diff to verify everything that you’re staging and committing. It’s also a good practice to have only one small feature per commit so it is easy to track via the commit log, what changes have been made. It’s better to have several small functional commits than one large commit. However, if you would like to save WIP code then it’s important to do them on a separate branch then rebase or perform a pull request which will squash them all into one commit.
Undoing a commit
For a local commit you undo it by
$ git reset HEAD~
Then you’ll need to add them and commit them again.
For a remote commit
$ git revert commitnumber
Saving local changes without committing
Sometimes you have something that’s a WIP progress that you would like to keep but don’t feel ready to commit yet. However, you need to pull the changes from the remote code repository. That’s when you use git stash. It saves your local changes for you to regain later.
$ git stash
$ git pull
$ git stash pop
Occasionally there will be conflicts during a git stash pop which you then need to resolve in each file. They’ll show up as something like the following:
<<<<HEAD
======
>>> LOCAL CHANGES
You’ll need to keep the changes that work best for you and delete the rest.
Then, it’s important to either merge the changes back to remote
$ git push
OR do a git reset so that the resolution between the pull and the stash are not staged.
$ git reset HEAD~
Merging a feature branch into master
When a feature is fully developed and tested, it could be useful to everyone. Then it’s a good idea to merge it back into master.
If you have access to the official repository then do the following:
$ git checkout master
$ git pull origin master
$ git merge branchname
$ git push origin master
Otherwise do a pull request
Possible git workflow
$ git diff
$ git stash
$ git pull
$ git stash apply/pop
$ git diff
$ git add filename
$ git commit -m commitmessage
$ git push
$ git status