Git cheatsheet¶
- Author:
GIT¶
See What Branch You’re On¶
Run this command:
git status
List All Branches¶
Note
The current local branch will be marked with an asterisk (*).
git branch
git branch -r
git branch -a
Create a New Branch¶
git checkout -b my-branch-name
You’re now ready to commit to this branch.
Switch to a Branch In Your Local Repo¶
Run this command:
git checkout my-branch-name
Switch to a Branch That Came From a Remote Repo¶
git pull
git checkout –track origin/my-branch-name
Push to a Branch¶
If your local branch does not exist on the remote, run either of these commands:
git push -u origin my-branch-name git push -u origin HEAD
Note
HEAD is a reference to the top of the current branch, so it’s an easy way to push to a branch of the same name on the remote. This saves you from having to type out the exact name of the branch!
If your local branch already exists on the remote, run this command:
git push
Merge a Branch¶
You’ll want to make sure your working tree is clean and see what branch you’re on. Run this command:
git status
First, you must check out the branch that you want to merge another branch into (changes will be merged into this branch). If you’re not already on the desired branch, run this command:
git checkout master
Note
Replace master with another branch name as needed.
Now you can merge another branch into the current branch. Run this command:
git merge my-branch-name
Note
When you merge, there may be a conflict. Refer to Handling Merge Conflicts (the next exercise) to learn what todo.
Delete Branches¶
git push origin –delete my-branch-name
git branch -d my-branch-name
git branch -D my-branch-name
Note
The -d option only deletes the branch if it has already been merged. The -D option is a shortcut for –delete –force, which deletes the branch irrespective of its merged status.
Tags¶
fetch remote tags :
1 2 |
|
List tags from remote .. code-block:: bash
git ls-remote –tags origin
And you can list tags local with tag
git tag
Even without cloning or fetching, you can check the list of tags on the upstream repo with git ls-remote:
git ls-remote --tags /some/url/to/repo "refs/tags/MyTag^{}"
git tag -a nombre-del-tag id-del-commit
git tag -d nombre-del-tag
git tag o git show-ref –tags
git push origin –tags
git tag -d nombre-del-tag y git push origin
:refs/tags/nombre-del-tag.
Change remote origin¶
git remote add origin git@gitlab.com:ambagasdowa/mkforms.git
git remote set-url origin git@gitlab.com:ambagasdowa/mkforms.git
Let’s start by explaining what a tag in git is¶
A tag is used to label and mark a specific commit in the history.It is usually used to mark release points (eg. v1.0, etc.).
Although a tag may appear similar to a branch, a tag, however, does not change. It points directly to a specific commit in the history and will not change unless explicitly updated.
You will not be able to checkout the tags if it’s not locally in your
repository so first, you have to fetch
the tags to your local
repository.
First, make sure that the tag exists locally by doing
# --all will fetch all the remotes.
# --tags will fetch all tags as well
$ git fetch --all --tags --prune
Then check out the tag by running
$ git checkout tags/<tag_name> -b <branch_name>
Instead of origin
use the tags/
prefix.
In this sample you have 2 tags version 1.0 & version 1.1 you can check them out with any of the following:
$ git checkout A ...
$ git checkout version 1.0 ...
$ git checkout tags/version 1.0 ...
All of the above will do the same since the tag is only a pointer to a given commit.
How to see the list of all tags?¶
# list all tags
$ git tag
# list all tags with given pattern ex: v-
$ git tag --list 'v-*'
How to create tags?¶
There are 2 ways to create a tag:
# lightweight tag
$ git tag v1.0
# annotated tag
$ git tag -a v1.0
How to delete tags?¶
Delete a local tag¶
$ git tag -d <tag_name>
Deleted tag <tag_name> (was 000000)
Note: If you try to delete a non existig Git tag, there will be see the following error:
$ git tag -d <tag_name>
error: tag '<tag_name>' not found.
Delete remote tags¶
# Delete a tag from the server with push tags
$ git push --delete origin <tag name>
How to clone a specific tag?¶
In order to grab the content of a given tag, you can use the
checkout
command. As explained above tags are like any other commits
so we can use checkout
and instead of using the SHA-1 simply
replacing it with the tag_name
Option 1:
# Update the local git repo with the latest tags from all remotes
$ git fetch --all
# checkout the specific tag
$ git checkout tags/<tag> -b <branch>
Option 2:
Using the clone command¶
Since git supports shallow clone by adding the --branch
to the
clone command we can use the tag name instead of the branch name. Git
knows how to “translate” the given SHA-1 to the relevant commit
# Clone a specific tag name using git clone
$ git clone <url> --branch=<tag_name>
git clone –branch=
``–branch`` can also take tags and detaches the HEAD at that commit in the resulting repository.
How to push tags?¶
``git push –tags``¶
To push all tags:
# Push all tags
$ git push --tags
Using the refs/tags
instead of just specifying the <tagname>
.¶
Why?
It’s recommended to use
refs/tags
since sometimes tags can have the same name as your branches and a simple git push will push the branch instead of the tag
To push annotated tags and current history chain tags use:
``git push –follow-tags``¶
This flag --follow-tags
pushes both commits and only tags
that are both:
Annotated tags (so you can skip local/temp build tags)
Reachable tags (an ancestor) from the current branch (located on the history)
From Git 2.4 you can set it using configuration
$ git config --global push.followTags true
Cheatsheet:
git tag tagname
git describe --tags
git tag --list tagname*
git tag --force //force commit chg
git push --tags --force //to the branch
git checkout tagname
git tag -d tagnum
git push origin :refs/tags/tagnum // or tagname