Skip to content

Latest commit

 

History

History
253 lines (196 loc) · 4.51 KB

File metadata and controls

253 lines (196 loc) · 4.51 KB

🧙‍♂️ Git Magic: Command Line Edition

Git Logo

🔧 Configure Your Git Persona

Before using Git, you need to set up your identity so that your commits are properly attributed to you.

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

If you want to set it only for a specific project, remove --global.

To verify your configuration:

git config --list

🛠️ Creating a New Repository

Steps:

  1. Create a new project directory:
    mkdir my_project
    cd my_project
  2. Initialize a Git repository:
    git init

This creates a hidden .git folder that tracks changes in your project.

To check if your repository has been initialized:

git status

📁 Adding Files to Git

Add files to the staging area:

  • Add a specific file:
    git add filename
  • Add all files in the directory:
    git add .

Commit changes:

git commit -m "Your commit message"

To check the current status of your repository:

git status

To view commit history:

git log

🌐 Connecting to a Remote Repository

Steps:

  1. Create a repository on GitHub, GitLab, or Bitbucket.
  2. Link your local repository to the remote repository:
    git remote add origin <remote_url>
  3. Verify the remote repository:
    git remote -v
  4. Push your local commits to the remote repository:
    git push -u origin master

🕵️‍♂️ Cloning a Repository

To download an existing repository from a remote server:

git clone <repository_url>

To clone into a specific directory:

git clone <repository_url> my_directory

🚨 Undoing Changes

Undo last commit (without losing changes):

git reset --soft HEAD~1

Undo last commit (discarding changes):

git reset --hard HEAD~1

Remove a file from the staging area:

git reset HEAD filename

Discard local changes in a file:

git checkout -- filename

🌿 Working with Branches

Create a new branch:

git branch new-branch

Switch to a branch:

git checkout new-branch

Create and switch to a branch in one step:

git checkout -b new-branch

Merge a branch into master:

git checkout master
git merge new-branch

Delete a branch:

git branch -d new-branch

🔄 Syncing and Updating

Pull latest changes from remote:

git pull origin master

Fetch updates without merging:

git fetch origin

Rebase to keep a clean commit history:

git rebase origin/master

🏷️ Tagging Commits

Create a tag:

git tag v1.0.0

Push tags to remote:

git push origin --tags

🌟 Stashing Changes

Save unfinished work:

git stash

List all stashes:

git stash list

Retrieve and apply the latest stash:

git stash pop

Apply a specific stash:

git stash apply stash@{0}

Drop a stash:

git stash drop stash@{0}

🧙‍♂️ Summary Table

Command Description
git config --global user.name "Name" Set global username
git config --global user.email "email" Set global email
git init Initialize a new repository
git add . Stage all changes
git commit -m "message" Commit staged changes
git remote add origin <url> Link remote repository
git push -u origin master Push code to master branch
git pull origin master Pull latest changes
git branch new-branch Create a new branch
git checkout new-branch Switch to a branch
git checkout -b new-branch Create and switch to a branch
git merge new-branch Merge a branch into master
git branch -d new-branch Delete a branch
git log View commit history
git status Check current state
git reset --soft HEAD~1 Undo last commit (keep changes)
git reset --hard HEAD~1 Undo last commit (discard changes)
git checkout -- filename Discard local changes
git stash Stash changes
git stash pop Apply stashed changes
git stash list List all stashes
git tag v1.0.0 Create a tag
git push origin --tags Push tags to remote
git fetch origin Fetch updates from remote
git rebase origin/master Rebase local branch

Now, you're ready to master Git like a pro! 🚀