Skip to content

Instantly share code, notes, and snippets.

@Linux4Life531
Created August 26, 2020 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Linux4Life531/e11a8c3ec3700c646befe3ff0cc89a71 to your computer and use it in GitHub Desktop.
Save Linux4Life531/e11a8c3ec3700c646befe3ff0cc89a71 to your computer and use it in GitHub Desktop.
Git and Git-Flow Command Line Cheat Sheet

Git Commands Cheat Sheet

This is a simple git cheat sheet that talks through the commands and the workflow

Installing

To install git on Linux use the following command:

$ sudo 'packageManagerName' install git

Example:

$ sudo apt install git

On windows or bash, you can use git-bash. Download here

Creating a git repository

So now you have git installed, you want to turn this folder into a git repo (Short for repository). To do this, we run the initialize command like this:

$ git init

If successful, the current folder has been turned into a git repo. This means we can start issuing git commands to it

Checking our version and status

To check our git version, we should simply use the following command

$ git version

To check the status of the current/parent folders, type:

$ git status

Adding a remote git repo

Once we have created our git repo locally, if we want to make this folder public, we must create a repo on a git server like github or gitlab. Once you have done that, copy the URL at the top and past it in here:

$ git remote add 'nameOfGitServer' 'url'.git

We can then see the added remote by typing

$ git remote -v

Adding Our Files To The Git Tracking System

Before we commit anything, we must add them to the staging area, where they are ready to be committed. To do this, we will use the following command:

$ git add 'filenames (Seperated by a space)'

To add all of the files, simply type the following:

$ git add . -A

Once you have ran one of these commands, you are ready to commit your files.

Committing Our Files

To commit our files, we just can just run the commit command

$ git commit

This will put us in a nano editor though, where we will type our message to say what changes have been made. To do this all through the command line, use:

$ git commit -m "My Message That Goes With The Commit"

Pushing Our Files To A Remote

To push our files to a remote repo, like the ones we added earlier, we can use the git push command, like this:

$ git push 'Remote Name' 'Branch'

Example:

$ git push github master

Branching Out

To create a new branch, you must use the branch command like this:

$ git branch 'nameOfBranch'

To see all of the branches, run this command:

$ git branch

To switch branches, use the checkout command like this:

$ git checkout 'nameOfBrach'

This means that any change we make in any editor goes towards that branch. To make and switch to a branch at one time, go like this:

$ git checkout -b 'nameOfBrach'

When we create a branch, we copy over the current branches files, but just rename the branch.

Merging those branches

To merge 2 branches, we will use the merge command, like this:

$ git merge 'nameOfBranch'

This will start a do a new commit with the merge.

Using Git-Flow For Your Workflow

You can use an open source project to organize your git workflow. Here is how to use it.

Initialization

To initialize a new repo with the basic branch structure, use:

git flow init [-d]

This will then interactively prompt you with some questions on which branches you would like to use as development and production branches, and how you would like your prefixes be named. You may simply press Return on any of those questions to accept the (sane) default suggestions.

The -d flag will accept all defaults.

Creating feature/release/hotfix/support branches

To list/start/finish feature branches, use:

$ git flow feature
$ git flow feature start <name> [<base>]
$ git flow feature finish <name>

For feature branches, the arg must be a commit on develop.

To push/pull a feature branch to the remote repository, use:

$ git flow feature publish <name>
$ git flow feature pull <remote> <name>

To list/start/finish release branches, use:

$ git flow release
$ git flow release start <release> [<base>]
$ git flow release finish <release>

For release branches, the <base> arg must be a commit on develop.

To list/start/finish hotfix branches, use:

$ git flow hotfix
$ git flow hotfix start <release> [<base>]
$ git flow hotfix finish <release>

For hotfix branches, the <base> arg must be a commit on master.

To list/start support branches, use:

$ git flow support
$ git flow support start <release> <base>

For support branches, the <base> arg must be a commit on master.

Taken from the README.md file on github

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