Home Git Handbook
Post
Cancel

Git Handbook

Trying to save time I spend revisiting the same git stackoverflow answers.

Git?

It is git.

Branching 101

Read more from here.

Working locally

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# list all the local branches
git branch

# list all the remote branches
git branch -a

# list all branches verbose
git branch -v -a

# just make new branch
git branch <branch>

# switch to branch
git switch <branch>

# delete branch
git branch -d <branch>

# force delete branch
git branch -D <branch>

# rename current branch
git branch -m <branch>

# make a new branch, switch current working directory
git checkout -b <branch>

# if branch already exists
git checkout <branch>

Working with Remote

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# list remote 
git remote

# list remote with links
git remote -v

# add new remote
git remote add <name> <url>

# add remove remote
git remote rm <name>

# rename remote
git remote rename <old_name> <new_name>

Reset local branch to match remote branch

1
2
3
git fetch origin
git branch -v -a
git reset --hard <remote>/<branch>

How do I check out a remote Git branch?

1
2
3
4
# create a new branch of name test
git fetch origin
git branch -v -a
git switch -c test origin/test

Squash last n commits into single commit

1
2
3
# squash last 2 commits into single commit
git reset --soft HEAD~2
git commit

Edit a previous commithere.

1
2
3
4
5
6
7
8
9
10
# suppose you want to edit commit `bbc643cd`
# ~ at the last is important
git rebase --interactive bbc643cd~

# change `pick` to `edit` of bbc643cd

# ammend your commit
git commit --all --amend 

git rebase --continue

Change the Windows CRLF to Unix LF

1
2
3
4
5
git config --global core.autocrlf false

git rm --cached -r .

git reset --hard
This post is licensed under CC BY 4.0 by the author.