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 | git config --global user.name "name" |
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)
- Create a repository in the github
- In the terminal, type
1 | git init |
Get a existed repository from remote repo
- Go the Github, and go the repositories
- Click the
Clone or download
, copy the web URL - 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 | # add the modification of the file to the stage area, which is ready to commit for the master branch. |
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 | # remove them from the cached area firstly |
Recommit
1 | # recommit with a new message |
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 | # to reset a file to a specific version |
Revert the repository
1 | git reset --hard <commits id>` |
Use git reflog
to see all the git actions before.
Branch
Create
1 | # create |
to get that branch
1 |
|
Merge
1 | # Merge branch with `branch_name` into current branch |
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 | git merge --strategy-option ours |
Rebase
Merge will create a new commit which reflects the merge action. Use rebase could just pick those different commits into other branches
1 | # Put the different commits in branch_names into current branch |
Sometimes we want to rebase with remote branch in order to push local changes
1 | git fetch |
Interact with remote repo
Configure remote repo
1 | # show remote repo url |
Push
1 | git push origin <local branch name>:<remote branch name to push into> |
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 | # check the settings |
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> |