git basic usage

Keywords: git


Git has three states, and your file may be in one of them: committed, modified, and staged. Therefore, the concept of three working areas of GIT project is introduced: git warehouse, working directory and staging area

configuration information

View configuration

# View configuration information
$ git config
$ git config

Modify configuration

# The configuration information needs to be configured after git is newly installed

# Configure name
git config --global '[name]'

# Configure email
git config --global '[email address]'

# This configuration is very important. When uploading GitHub and gitlib in the future, the uploaded name and email will be displayed, 

# The user name and email address are variables of the local git client and do not change with the GIT library.
# Each commit will be recorded with the user name and mailbox.
# github's contributions statistics are based on mailboxes.

Add remote warehouse locally

git remote add origin remote warehouse address

Set ignore file or directory

A new one has been created.gitignore
# Add file paths to ignore

# Ignore pycharm version information

Create project

git init [project-name]

git clone [url]
from github,gitee Grab item on

basic operation

File add

View file status
git status

Submit to temporary storage
git add .

git add file1 file2

Add all untracked files
git add -A

git commit -m '[dscriptive message]'

Add all untracked files and submit with
git commit -a

Upload to server
git push

File deletion

git pull origin master
 Download to local

Delete the workspace file and put the deletion into the staging area
git rm [file1] [file2] ...

If you want to delete a previously modified and put it into the staging area, you must use forced deletion
git rm -f <file>


List branches

List local branches

git branch

  • main

List remote branches

git branch -r

remotes/origin/HEAD -> origin/master

List all branches

git branch -a

  • main
    remotes/origin/HEAD -> origin/master

Create and switch branches

Create local branch

New branch
git branch branch_name

Create and enter branch
git branch -b branch_name
-b create and switch branches

Switch branch

git checkout branch_name

Create remote branch

git push origin [name]

No remote branch > associate local branch to remote origin

Push local branch_ Name (before colon) branch to remote origin branch_ Name (after colon) branch (not automatically created)

git push origin branch_name:branch_name

git push origin master_1:master_1

Delete branch

Delete local branch

git branch -d [branch-name]

Delete remote branch

git push origin --delete <BranchName>

Merge branch

Before merging, you must go to the merged branch
For example, merge the dedug branch to the master

git checkout master
git merge debug

Merge conflicts need to be resolved manually

Modify and set default main branch

git branch --set-upstream-to=origin/master—_1 master


# Get history of modifications
git log

# Brief log
git reflog

git log --follow [file]

View the modified contents of the file
git diff 

git show


git reset [file]
Undo file

git reset --hard  Rollback number

git reflog
git reset --hard Rollback number

Main problems

"no upstream branch" problem when push ing after creating a new branch in git
Establish local and remote branch connections

git branch --set-upstream-to=origin/master master
git pull
git config --global push.default current
git push -u

push.default in Git can specify the remote branch of the default push when no remote branch is explicitly specified. Its value can be:

  • Nothing - the push operation is not valid unless a remote branch is explicitly specified
  • current - push the current branch to the remote branch with the same name. If the remote branch with the same name does not exist, the branch with the same name will be automatically created (both central and non central workflows are applicable)
  • upstream - push the current branch to its upstream branch (usually used for central workflow)
  • simple - simple and upstream are similar (usually used for central workflow), with one difference. Simple must ensure that the local branch and its remote upstream branch have the same name, otherwise the push operation will be rejected
  • matching - push all branches with the same name that exist at both local and remote ends
There is no tracking information for the current branch

Method 1

git checkout Specify branch
git pull  origin Specify branch

Method 2

git branch --set-upstream-to=origin/newmaster  newmaster

Posted by celebx on Tue, 30 Nov 2021 11:16:13 -0800