Skip to content

Latest commit

 

History

History
244 lines (181 loc) · 7.63 KB

06-operations-overview.md

File metadata and controls

244 lines (181 loc) · 7.63 KB

Up | Content | Intro | Concepts | Operations | Dictionary


Git Operations Overview

Git Dataflow


1. Help

Get help about any given command, guide or configuration

$ git help
$ git help --guides
$ git help --config

2. Create

Create a local or remote repository.

$ git clone https://github.com/user/demo.git

3. Config

Configure the local repository.

# =============================================================================
# Configure and verify the user email
# =============================================================================

$ git config --global user.email "user@mail.com"
$ git config user.email

# =============================================================================
# Create aliases
# =============================================================================

$ git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
$ git config --global alias.type 'cat-file -t'
$ git config --global alias.dump 'cat-file -p'

# =============================================================================
# Use aliases
# =============================================================================

$ git dump HEAD

4. Track

Save, track and label changes.

# =============================================================================
# Add changes
# =============================================================================

$ echo 123 > README.md

# =============================================================================
# Start tracking
# =============================================================================

$ git add *

# =============================================================================
# Save the changes
# =============================================================================

$ git commit -m 'Commit message'

# =============================================================================
# Label a specific revision
# =============================================================================

$ git tag V1.0 HEAD
$ git tag

# =============================================================================
# Show the history
# =============================================================================

$ git log

5. Branch

Create and merge branches, stash changes and move the HEAD.

# =============================================================================
# Create a branch and switch to it
# =============================================================================

$ git branch test
$ git switch test

# =============================================================================
# Do some work
# =============================================================================

$ echo 1 > README.md
$ git add *
$ git commit
$ echo 2 > README.md
$ git add *

# =============================================================================
# Try to switch to main without committing changes
# =============================================================================

$ git switch main

# =============================================================================
# Save changes to the stash temporarily
# =============================================================================

$ git stash
$ git stash list
$ git stash show

# =============================================================================
# Now switch to main
# =============================================================================

$ git switch main

# =============================================================================
# Switch back to test, restore changes and commit
# =============================================================================

$ git switch test
$ git stash pop
$ git commit -m 'File changed in branch'

# =============================================================================
# Merge changes from test to main
# =============================================================================

$ git switch main
$ git merge test

# =============================================================================
# Detach the HEAD
# =============================================================================

$ git checkout V1.0

# =============================================================================
# Attach the HEAD
# =============================================================================

$ git switch main

6. Sync

Collaborate with other developers.

# =============================================================================
# Write changes to remote repository
# =============================================================================

$ git push
$ git push origin test

# =============================================================================
# Read changes from remote repository
# =============================================================================

$ git pull
$ git pull origin test

7. Revert

Revert changes to the project files using the index or the local commit history.

# =============================================================================
# Restore file from index
# =============================================================================

$ del README.md
$ git status
$ git restore *
$ git status

# =============================================================================
# Restore index from commit history
# =============================================================================

$ echo ABCD > README.md
$ git add *
$ git status
$ git restore --staged
$ git status

8. Inspect

Inspect the commit history, check the author of the modifications, get the repository status and other operations.

# =============================================================================
# Get the last 5 entries
# =============================================================================

$ git log -5

# =============================================================================
# Check the status of the working tree
# =============================================================================

$ git status --short

# =============================================================================
# Check the content of an object
# =============================================================================

$ git cat-file -p HEAD

# =============================================================================
# Show info about branches
# =============================================================================

$ git branch -av

# =============================================================================
# Show info about remote references
# =============================================================================

$ git remote -v