3 minutes
My essential cheatsheet for Git
Branches
Create
To create local branch type:
$ git checkout -b my-local-branch-name
Next command, will create remote branch (and commit changes, if any exists on local branch):
$ git push origin my-local-branch-name
(origin
means, “your original (remote) repo)
But it will not setup tracking remote branch with your local❗ To do this, you have to use u
flag:
$ git push -u origin my-local-branch-name
If you want to create new local branch with tracking existing remote:
$ git checkout -b <your-new-local-branc> origin/<existing-remote-branch>
Switch to existing branch
$ git checkout <local-or-remote-branch-name>
List branches
To list your local branches and check current branch (marked with: *
) type:
$ git branch
vv
flag is usefull if you want to find out, is local branch tracking remote and whichone. It’ll show you last commit hash and message
$ git branch -vv
You can also list remote branches. Just add r
flag:
$ git branch -r
If you want to list all branches (local and remote), use a
flag:
$ git branch -a
If you combine two last flags (a
and vv
), you’ll able to show all the information, i wrote in this “sub-paragraph". So, let’s try - type:
$ git branch -avv
Delete
REMEMBER! You cannot delete branch, if you checked out (you’ll get error: error: Cannot delete branch '<your_local_branch>' checked out at '<local_path>'
). Change your current branch, and then try again.
To delete local branch type:
$ git branch -d <your-local-branch-name>
If branch contains any not pushed commits, you will get error (error: The branch '<your_local_branch>' is not fully merged.
). If you won’t push it, you can add f
flag
$ git branch -df <your-local-branch-name>
or instead d
, use D
$ git branch -D <your-local-branch-name>
When you want to delete remote branch, you don’t need to delete local branch, which is tracking remote. If you type command:
$ git push origin -d <remote-branch-name>
Then, if you check your local branches (git branch -vv
) and you had local tracked branch, you will see gone next to deleted remote branch:
master last_commit_hash [origin/master] Last commit on master
* <local-branch> last_commit_hash [origin/<remote-branch>: gone] Last commit on this branch
If command git branch -a
still shows remote branch(es), use command:
$ git fetch origin --prune
Others
If you need use other name/email for specific repo:
Change dir to your cloned repo location dir
$ cd location_of_your_git_directory
Set your nick/name/surname using command:
$ git config user.name "Kalik Dev"
Now put your email:
$ git config user.email "[email protected]"
Optionaly, you can use this command git config -l
to check, if changes have been applied (if you are Linux user or you using WSL/Git Bash (aka MinGW), you can add | fgrep user.
, to filter records):
$ git config -l | fgrep user.
Console will show you something like that:
user.name=Marcin K.
user.email=[email protected]
user.name=Kalik Dev
user.email=[email protected]
You will see your default and new name and email, but git will use “the last one” records!
PAY ATENTION!
❗ It’s email
, not mail
. If you add record user.mail
, it won’t work ❗