Git

Tags

Tags are used for creating stable releases.

Create Tags

git tag <tag name> [-m <tag description>]

e.g. git tag LABEL_REL_1_0

Once the tag is created, you need to push the tag up to the master repository. By itself, push doesn't send the tags up, you also need to tell it to include the tags in the push by appending the --tags flag:

git push --tags

If you don't want to push all your tags, you can also be specific:

Example: git push origin tag 7.x-1.0

To check and confirm remote tags, the command is

git tag -l [<pattern>]

List tags with names that match the given pattern (or all if no pattern is given). Typing "git tag" without arguments, also lists all tags.

git tag -n

The -n flag displays the first line of the annotation message along with the tag, or the first commit message line if the tag is not annotated.

Deleting Tags

Tag with name "tagname" (again, the second command is only necessary if you already pushed it

git tag -d <tagname>

git push origin :<tagname>

Squash

Branches

Working with branches is similar to working with tags, but branches are used for development releases.

First create the new branch and check it out:

git checkout -b <brnach name>

e.g. git checkout -b 7.x-2.x

Create branch\checkout from tag

git checkout -b <branch name> <tag name>

git checkout -b newbranch v1.0

git branch newbranch v1.0

The above command will just create the new branch, not check it out.

Once the branch is created locally, it can be pushed up to the remote repository:

git push origin <branch name>

git push origin 7.x-2.x

To work with this branch:

git checkout <branch name>

git checkout 7.x-2.x

To see what branches you currently have:

git branch -v

The branch with the asterisk next to it is the active branch:

git branch -v

* 7.x-2.x 170eb10 Initial commit.

master 170eb10 Initial commit.

Deleting a branch

Branch with name "branchname" (the second command is only needed if you already pushed it

git branch -d <branchname>

git push origin :<branchname>

If for some reason you have a branch and a tag that are named the same (you shouldn't, but we're talking about a mistake here anyway), you can push with the following commands:

git push origin :refs/heads/branchname

git push origin :refs/tags/tagname

To check and/or confirm available remote branches command is

git branch -r

Upstream Branches

Working with upstream non-master branch

Clone your private repo [I have cloned my forked repo, you have to clone your own forked repo]

$ git clone https://github.com/deepagargit/crud-service.git

$ cd crud-service

$ git branch -a

* master

remotes/origin/HEAD -> origin/master

remotes/origin/master

$ git remote -v

origin https://github.com/deepagargit/crud-service.git (fetch)

origin https://github.com/deepagargit/crud-service.git (push)

Set remote

$ git remote add upstream https://github.com/deepagarmaster/crud-service.git

$ git remote -v

origin https://github.com/deepagargit/crud-service.git (fetch)

origin https://github.com/deepagargit/crud-service.git (push)

upstream https://github.com/deepagarmaster/crud-service.git (fetch)

upstream https://github.com/deepagarmaster/crud-service.git (push)

Fetch upstream branches

$ git fetch upstream

remote: Counting objects: 13, done.

remote: Total 13 (delta 4), reused 4 (delta 4), pack-reused 9

Unpacking objects: 100% (13/13), done.

From https://github.com/deepagarmaster/crud-service.git

* [new branch] 0.1_rel -> upstream/0.1_rel

* [new branch] master -> upstream/master

* [new tag] LABEL_0_1_REL -> LABEL_0_1_REL

Create local branch from upstream/0.1_rel

$ git checkout -b 0.1_rel_local upstream/0.1_rel

Branch 0.1_rel_local set up to track remote branch 0.1_rel from upstream.

Switched to a new branch '0.1_rel_local'

Do work on '0.1_rel_local'

Commit and push in '0.1_rel_local'

Raise the merge request from '0.1_rel_local' to upstream/0.1_rel

Revert a commit

git revert <SHA of commit>

Cheat Sheet

Using Git Bash or the Terminal navigate to the actual project folder. If you are using Git Bash you can right-click the project folder and select “Git Bash Here” and it will start you in that working directory.

git init

This will create a .git repository in your project. A repository or “repo” is a collection of all the changes you’ve made to your project over time and will build a history of these changes. This is the first thing you want to do with a new project.

git config --global user.name "Your Name"

git config --global user.email "yourEmail@mail.com"

This sets up your information that is used every time you commit. This only needs to be done once when you first install Git.

git add filename.extension

Replace “filename.extension” to whatever file you are trying to add like “index.html”. This will add the file you specify to what is called a “staging area” or index. Think of the staging area like a section where things are getting set up to be moved to your repository.

git add .

If you want to add everything from the project folder to the staging area this command will do that instead of having to add each file one by one.

git add *.html

If you want to add all .html files to your staging area this would be the command to use. The extension can be changed to whatever you want.

git status

Shows what has already been added to the staging area and what files have been changed that need to be added to the staging area.

git reset filename.extension

Removes specified file from the staging area.

git rm --cached filename.extension

This will remove the file from the staging area and sets it to be untracked.

git commit -m "Description of the commit"

Takes the files from your staging area and commits them to your local repository. In quotes will be a brief description of what was changed with each commit. Try to describe the commit in brief detail such as “fixed bug where user name wasn’t updating” rather than a commit message like “some changes.”

touch .gitignore

This will create a file called .gitignore. You can open that file with a text editor and write the name of files or folders you want to be ignored from your repository. Ignored files won’t show up when you run git status to prevent you from committing files you’ve previously said you don’t want to commit or even know about their changes.

git branch branchName

Creates what is called a branch. A branch is a direct copy of your codebase from previous branch you were on (often the master branch).

git checkout “branchName”

Will allow you to checkout the branch you created and work within that branch. You can make any changes to your code that you want here. When it’s ready, you can commit your code and push the branch to GitHub (see below) or you can delete the branch if something goes wrong or you decide you don’t need that feature or bug fix any longer.

git merge branchName

While inside Master you can use this command to take the commits from the branch you were working in and merge them together with the main repository.

git remote add origin https://github.com/userName/project.git

This adds the location of your remote repository. Everything up until now has been on your local repository on your computer. You will need to go to your GitHub account and create a new remote repository where you will be able to push your local repository. After you created your remote repository you will be provided with a link and that link is the location you will want to use in the above command.

git remote

List of your remote repositories that have been associated with your project.

git push -u origin master

This will push your local repository to your remote repository. This command only needs to be written like this when you do it for the first time.

git push

This is what you will use to push your code to GitHub after your initial push.

git clone https://github.com/userName/project.git

If you don’t have your project on the computer you’re working with this will allow you to clone (or download) the entire project into the directory you are working.

git pull

If you are working on the same codebase with other people, this command will allow you to pull the latest version from the remote repository and update your local version so you can work with the latest updates as their changes enter the codebase.

References