Skip to content

Instantly share code, notes, and snippets.

@alemohamad
Last active October 18, 2015 22:03
Show Gist options
  • Select an option

  • Save alemohamad/0a94915ad566a795e54f to your computer and use it in GitHub Desktop.

Select an option

Save alemohamad/0a94915ad566a795e54f to your computer and use it in GitHub Desktop.

Git Workflow

# 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 master

Something to read about this commands:

Some more tips

  1. Be up to date
  2. Every commit is "forever"
  3. View a commits/branches graph of our repo work
  4. Renaming your branch
  5. Where am I?
  6. Clone a project with only a specific branch
  7. Retrieve a remote branch to work locally inside that branch
  8. Delete a local branch
  9. Stashing changes

Be up to date

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 pull

Every commit is "forever"

Please 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 status

View a commits/branches graph of our repo work

We can view a graph of our commits and branches in our command line with the following paramters:

$ git log --oneline --graph --all --decorate

Renaming your branch

Maybe 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>

Where am I?

If you can't remember in which branch you are positioned (or how many branches you have) you can use this:

$ git branch

And you also can be aware fo every branches, both locally and online:

$ git branch --all

Clone a project with only a specific branch

If 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>

Retrieve a remote branch to work locally inside that branch

git checkout -b <branch_name> origin/<branch_name>

Delete a local branch

We do not recommended to do this if you are unsure about it, but here it is:

$ git branch -d <branch-name>

Tags in Git

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 --tags

But now, we want to sync the created tags to our online repo (maybe GitHub):

$ git push --tags

There 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 inconsistencies

Stashing changes

Wanting to save current changes, but not making a commit? Do a pause on those files instead.

$ git stash

Wanting to get the "paused" changes?

$ git stash pop

Want 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 stash

Want to delete all your stashes because they are a mess?

$ git stash clear

More info:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment