Skip to main content

Git

Basic Git workflow covering the essential commands for typical tasks.

Basic workflow

CommandDescription
git clone <repository-url>

Clone a remote repository.

git status

Show the current state of the working directory and staging area.

git add <file>

Stage specific file changes.

git add .

Stage all changes in the current directory.

git commit -m "message"

Commit staged changes with a descriptive message.

git push origin <branch>

Push changes to the specified remote branch.

git pull origin <branch>

Pull changes from the specified remote branch.

git branch <new-branch>

Create a new branch.

git switch <branch>

Switch to an existing branch.

git merge <branch>

Merge the specified branch into the current branch.

git branch -d <branch>

Delete a local branch.

git push origin --delete <branch>

Delete a remote branch.

Make and push changes.

CommandDescription
git checkout -b <"my-feature">

Create and switch to a new branch called "my-feature".

git commit -m <"feat: my feature description">

Commit changes with a message describing the new feature.

git push

Push changes to the current branch on the remote repository.

git push -u origin <"my-feature">

Push the "my-feature" branch to the remote and set the upstream tracking.

Updating a branch

git checkout

CommandDescription
git fetch

Fetch updates from the remote repository without merging.

git checkout your-branch-name

Switch to the specified branch.

git merge origin/main

Merge changes from the main branch into the current branch.

git rebase origin/main

Rebase the current branch on top of the main branch.

git add resolved-file

Stage the resolved file after fixing any conflicts.

If Merging

If Merging

git merge --continue

Continue the merge process after resolving conflicts.

Rebase

Rebase

git rebase --continue

Continue the rebase process after resolving conflicts.

git push origin your-branch-name

Push changes to the specified branch on the remote repository.

git push origin your-branch-name --force-with-lease

Force push changes to the branch while ensuring safety against overwriting.

git switch

CommandDescription
git fetch

Fetch updates from the remote repository without merging.

git switch your-branch-name

Switch to the specified branch.

git merge origin/main

Merge changes from the main branch into the current branch.

git rebase origin/main

Rebase the current branch on top of the main branch.

git add resolved-file

Stage the resolved file after fixing any conflicts.

git merge --continue

Continue the merge process after resolving conflicts.

git rebase --continue

Continue the rebase process after resolving conflicts.

git push origin your-branch-name --force-with-lease

Force push changes to the branch while ensuring safety against overwriting.

git fetch
git switch your-branch-name
git merge origin/main # or git rebase origin/main
# Resolve conflicts if needed
git add resolved-file
# if merging
git merge --continue
# if rebasing
git rebase --continue
git push origin your-branch-name --force-with-lease # if rebasing

Resolving conflicts

git checkout

git status
# Open and edit conflicted files
git add <file>
git commit
# If rebasing, continue with
git rebase --continue
git push

git switch

git switch <branch-name>
git merge <branch-to-merge>
git status
# Resolve conflicts in files
git add <file>
git commit
git push