Git

Git is a powerful version management system. This posts captures the common used commands.

Initialization

In order to create a git folder, we need to go the the folder where we save our code and open the terminal.

Set name and email

Use

1
2
git config --global user.name "name"
git config --global user.email "email"

to set the local git account.

Use

1
git config -l

to see all the settings of git

Create a git repository and upload it to your remote repo (github, Azure DevOps, and etc)

  1. Create a repository in the github
  2. In the terminal, type
1
2
3
4
5
git init
git add .
git commit -m <commit message>
git remote add origin git@github.com:<username of github>/<name of the repository>.git
git push -u origin master

Get a existed repository from remote repo

  1. Go the Github, and go the repositories
  2. Click the Clone or download, copy the web URL
  3. In the terminal, type
1
git clone <your web URL> <your local project name>

Save a commit and revert

Working Directory and Repository (stage, master)

All the modification (note. git is tracking the modification to the files instead of the files itself) to the files are in the working directory. There are also stage area and branch area. These two are called repository.

1
2
3
4
5
# add the modification of the file to the stage area, which is ready to commit for the master branch.
git add <file>

# commit the modifications in the stage area to the master branch.
git commit -m "message"

About .gitignore

In Windows, you need to create a txt file with name of ignore, then use the following command

1
ren gitignore.txt .gitignore

to create the gitignore file

Sometimes when you add some files to the .gitignore file, but those files are still in your repository. That is because those files are already in stage area or the master area before, adding the file to the .gitignore cannot remove the file from the master branch.

To remove that file from the branch,

1
2
3
4
5
# remove them from the cached area firstly
git rm -r --cached .

# readd all files required to be tracker
git add .

Recommit

1
2
3
4
5
6
7
8
9
10
11
# recommit with a new message
git commit --amend -m "notes"

# recommit with previous message
git commit --amend --no-edit

# combine latest 3 commits together
git rebase -i HEAD~3

# need to enforce push if you already pushed before. This is a little dangerous
git push -f

Tag

Use

1
git tag "version"

to tag a commit.

Use

1
git show <tagname>

to see the commits information of a specific tag.

Revert a file

1
2
3
4
5
6
7
8
9
10
11
# to reset a file to a specific version
git checkout <commit_id> -- <file>

# to remove the file from the stage area to the working directory.
git reset HEAD <file>

# to revert the file being tracked in the working directory as the last commits version.
git checkout -- <file>

# abort all file changes in the working directory
git clean -fxd

Revert the repository

1
git reset --hard <commits id>`

Use git reflog to see all the git actions before.

Branch

Create

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# create
git checkout -b <branch>

# check local branch
git branch

# check all branch including remote branch
git branch -a

# delete branch
git branch -d <branch>

# to get a remote branch
git fetch origin <branch>
git checkout branch

to get that branch

1
2
3
4
5
6
7
8
9
10
11

### Git stash

Sometimes we need to switch to another branch and we haven't commit current changes. We could use

``` Bash
# to save the current working content.
git stash

# to recover the previous work.
git stash pop

Merge

1
2
# Merge branch with `branch_name` into current branch
git merge <branch_name>

Sometimes there will be conflict from ours work and theirs work. In this way, we need to manually adjust the code the solve the conflict. Then use git add . to indicate all the conflicts are solved.

In some special cases, like in Unity, some files cannot be auto-merged due to the format of file. So we may want to keep one version of files directly. For example, if we want to keep our own files when there is conflict, we need to type

1
2
3
4
git merge --strategy-option ours

# or we can choose
git merge --strategy-option theirs

Rebase

Merge will create a new commit which reflects the merge action. Use rebase could just pick those different commits into other branches

1
2
# Put the different commits in branch_names into current branch
git rebase <branch_name>

Sometimes we want to rebase with remote branch in order to push local changes

1
2
git fetch
git rebase origin master

Interact with remote repo

Configure remote repo

1
2
3
4
5
6
# show remote repo url
git remote -v

# remove or add remote repo
git remote remove origin
git remote add origin <remote url>

Push

1
2
3
4
5
6
7
8
9
git push origin <local branch name>:<remote branch name to push into>

# push current branch to the remote branch
git push origin <remote branch name to push into>

# link current branch to a specific branch
git push --set-upstream origin <remote branch>
# nex time just need
git push

Note that, git push has two kinds. One is called simple, which is just pushing the current branch to the corresponding remote branch. Another one is called matching, which is pushing all local branches to corresponding remote branches. You can use the following to check and change the method

1
2
3
4
5
6
7
# check the settings
git config -l

# set the method
git config --global push.default matching
# or
git config --global push.default simple

Pull

Get the update from the origin repo. git pull is the combination of git fetch and git merge

1
git pull origin <local branch name>