# 1. Let's create a new feature branch
$ git checkout -b <name_of_new_branch>
# 2. We do some work in that branch (with all it's commits)
$ git commit -m "Here's a message of what I did"
# 3. Push your branch work to GitHub (this is your backup)
$ git push origin <branch>
# 4. Before moving our work to master, we should test it in our branch...
$ git pull origin master
# 5. ...and then merge it with our branch
$ git merge --no-ff master
# 6. Here we should fix every inconsistencies that could appear, and fix our branch code if there's a new problem
# 7. Now we switch back to master...
$ git checkout master
# 8. ...and merge our branch (from <branch> to master)
$ git merge --no-ff <branch>
# 9. Push our changes in master to GitHub, so everyone can use your brand new code
$ git push origin masterSomething to read about this commands:
- Be up to date
- Every commit is "forever"
- View a commits/branches graph of our repo work
- Renaming your branch
- Where am I?
- Clone a project with only a specific branch
- Retrieve a remote branch to work locally inside that branch
- Delete a local branch
- Stashing changes
Always remember to pull the content of the online repo to your local repo. You should do this at least once at the beginning of every day.
$ git pullPlease check your changes twice or thirce before doing a commit, and verify that your staged files are the ones that you want to commit. We don't want any garbage in our project repo. Also the message is important to, so be descriptive (don't write a book, but explain what you did there). This will help you to make better git history, and with that we won't be making 7 commits in 5 minutes, like a backup system. That could bother your team big time. :P
One best pratice for this is to use the following command before you do anything:
$ git statusWe can view a graph of our commits and branches in our command line with the following paramters:
$ git log --oneline --graph --all --decorateMaybe you were working with a feature branch, and you noticed that the main concern about this changes are other. So (for readability reasons) you want to change that branch name. Try to not depend on this command, but you can change the branch name with this:
$ git branch -m <original-branch-name> <new-branch-name>There's something else we have to do. If we're tracking this branch with another one on our online repository, we have to point this renamed branch with the renamed branch that has to be online. We can re-track this branch with this:
$ git branch <branch-name> -u origin/<branch-name>If you can't remember in which branch you are positioned (or how many branches you have) you can use this:
$ git branchAnd you also can be aware fo every branches, both locally and online:
$ git branch --allIf you want to start a project from a branch (not master branch), you can use the following clone command:
$ git clone -b <branch-name> --single-branch <project-git-url>git checkout -b <branch_name> origin/<branch_name>We do not recommended to do this if you are unsure about it, but here it is:
$ git branch -d <branch-name>If you want to create a tag, so you can mark important versions, you can use the following command:
$ git tag -a <tag-version> -m "Custom message"
# Example: $ git tag -a v1.0 -m "This is a major version."Now, we can see which tags do we have in our local repository:
# show local tags
$ git tag -ln
# show remote tags of the project
$ git ls-remote --tagsBut now, we want to sync the created tags to our online repo (maybe GitHub):
$ git push --tagsThere could be a time, that we want to delete a tag of our local repository:
$ git tag -d <tag-version>And then we would like to sync that deleted tag, so it doesn't exists for the rest of the team:
$ git push origin :<tag-version>Do you wish to fetch a tag from the online repository, so you can have that tag in your local repository? This is not important to have, if the tag exists at the online repository, but you can do the following:
$ git fetch origin <tag-version>:<tag-version>
# the <tag-version> must be the same, to avoid inconsistenciesWanting to save current changes, but not making a commit? Do a pause on those files instead.
$ git stashWanting to get the "paused" changes?
$ git stash popWant to see how many saved stashes you have?
# You shouldn't do this often
$ git stash list # list all stashes
$ git stash apply stash@{1} # get specific stash (doesn't delete it)
$ git stash drop stash@{1} # delete that specific stashWant to delete all your stashes because they are a mess?
$ git stash clearMore info: