Post

Git Cheatsheet

Use the power and structure of GIT in your web development workflow with these helpful Git commands.

Gits just one of those tools you need to know pretty damn well in order to hang with modern day web developers. Once your using git youll wonder how you ever worked without it. Truelly an essential modern day dev tool.

Revert file to last committed state

Git has many useful functions. One of my favourites is to scrap the work ive just done on the file I currently working on ( because I’ve gone down the wrong path or screwed it up) and start again from the files last committed state.

Functions like this give me peace of mind as a developer. If I screw up my file I can easily revert it.

Revert a file to the last committed state

1
git checkout pathtofile/filename.ext

Revert a file to the last committed state using fugitive

Fugitive is a vim plugin that integrates Git into the vim code editor. It’s super handy as you don’t need to change windows to do git functions like reverting changes to a file.

open the file in vim

1
git checkout .

Git Branch

See Git Branch Cheatsheet

Git Remote

Add a Remote:

1
git remote add <name> <url>

List Remotes:

1
git remote -v

Rename a Remote:

1
git remote rename <old_name> <new_name>

Remove a Remote:

1
git remote rm <name>

Show Remote URL:

1
git remote get-url <name>

Set Remote URL:

1
git remote set-url <name> <new_url>

Fetch Changes from a Remote:

1
git fetch <remote_name>

Push Changes to a Remote:

1
git push <remote_name> <branch_name>

Deploy to remote server using Git Push

deploy to server using git push

Pull Changes from a Remote:

1
git pull <remote_name> <branch_name>

Show Remote Branches:

1
git branch -r

Track a Remote Branch:

1
git checkout -t <remote_name>/<branch_name>

Delete a Remote Branch Locally:

1
git branch -d <branch_name>

Delete a Remote Branch:

1
git push <remote_name> --delete <branch_name>

git pull

The git pull command is used to update your local repository with changes from a remote repository. It performs a git fetch followed by a git merge. Here’s a basic usage:

1
git pull <remote> <branch>

for example

1
git pull origin main

Git push

Basic Push:

This command pushes changes from your local branch to the corresponding remote branch.

1
git push

Push Specific Branch:

Replace <remote_name> with the name of the remote repository (usually origin) and <branch_name> with the name of the branch you want to push.

1
git push <remote_name> <branch_name>

Push All Branches:

Pushes all branches to the remote repository.

1
git push --all

Push Tags:

Pushes all tags to the remote repository.

1
2
git push --tags

Force Push:

Forces the push operation, overwriting remote changes with your local changes. Use with caution.

1
git push --force
1
git push --force origin your-branch-name

Push a New Branch:

1
git push -u <remote_name> <branch_name>

Pushes the specified branch to the remote repository and sets it to track the upstream branch.

set-upstream

1
git push --set-upstream origin feature-branch

In Git, git push –set-upstream (or git push -u for short) is used to set the upstream (or tracking) branch for the current local branch. This means that it establishes a relationship between your local branch and a remote branch so that future operations can be simplified.

Push with Force and Lease:

A safer alternative to –force, which checks if the remote ref is updated. Helps prevent unintentional overwriting of changes.

1
git push --force-with-lease

Push a Commit to Different Branch:

Pushes a specific commit to the specified branch on the remote repository.

1
git push <remote_name> <commit_hash>:<branch_name>

Push to Multiple Repositories:

Adds multiple push URLs for a remote repository. Helpful for pushing to multiple remotes simultaneously.

1
git remote set-url --add --push <remote_name> <url>

Pushing to Multiple Repositories

Suppose you have a repository hosted on both GitHub and GitLab, and you want to push changes to both:

1
2
3
4
5
git remote set-url --add --push origin git@github.com:username/repository.git

git remote set-url --add --push origin git@gitlab.com:username/repository.git

git push origin master

This configures your origin remote to have two push URLs, one for GitHub and one for GitLab. Then, when you push changes to origin master, Git pushes to both repositories.

Remember to replace placeholders like <remote_name>, <branch_name>, and <url> with actual names and values relevant to your Git repository setup. Always ensure you understand the consequences of your push actions, especially when using force options.

Pushing all branches

If you want to push all branches to each repository, you can use the --all flag:

1
git push --all repo_name

git diff

see what files you are about to push

1
git diff --stat --cached origin/main

Remove the folder from tracking:

1
git rm -r --cached your_folder
This post is licensed under CC BY 4.0 by the author.