Skip to content

Git command cheat sheet that serves as a quick reference for essential commands such as navigating repositories, managing commits, and streamlining collaboration with branches and pushes.

Notifications You must be signed in to change notification settings

rusenaite/GitCommandFondue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Git Command Fondue before forgot it again

Abstract

git init
git remote add origin <you-git-link>
git add .
git commit -m "Initial commit"
git branch -M main
git push -u origin main

Before anything: Change directory into the relevant root directory for the project
init - initialize repository
origin - default given name to remote repository, can be named differently

git add .
git commit -m "Initial commit"
  • git add . - add all changes to the staging area
  • git add <filename> - add specific file changes to the staging area
  • git commit - creates new commit from the changes in the staging area
  1. Undo git commit. Changes still exist in the working tree (project's files and directories) + index (staging area) (--cached):

    git reset HEAD^ --soft
    

HEAD - points to the tip of that branch (most recent commit)
^ - one commit before
HEAD^ - refers to the parent commit of the current HEAD
--soft - Changes made in the last commit are uncommitted and goes back to the staging area so that you can modify them before committing again.

  1. Undo git commit + git add + unstage. Changes still exist in the working tree:

    git reset HEAD^ --mixed
    

--mixed - resets the current branch and index to the specified commit but does not modify the working tree. The changes that were previously staged are not carried over, so you'll have to stage them again using git add.

  1. Undo git commit and remove changes. Removes the most recent commit and all of the changes made in that commit.

    git reset HEAD^ --hard
    

--hard - remove the commit and all the changes introduced in that commit will not be recoverable through Git. Changes are gone from the working tree, index, and local repository. This option resets everything back to the state of the last commit.

Create a new commit that reverses the changes made by specified commit, without applying that it.

git revert -n <sha>

SHA - commit identifier in Git. Stands for Secure Hash Algorithm. It's a 40-character string that identifies the state of a particular commit, tree or blob.
<sha> - your commit's SHA
-n - flag that allows you to make additional changes to the files before committing them

Example:

If you want to undo commit with SHA a1b2c3, command git revert -n a1b2c3 will undo the changes introduced by the commit a1b2c3. Changes will be staged, you can review them before committing.

git reset
git status <directory-name>
git commit -m "Add new feature"
git fetch origin
git merge origin/main
git pull origin
git push origin main
git checkout main
git pull
git merge <feature-branch>

* Resolve any conflicts and then push the changes to the remote main branch *:

git add .
git commit
git push
  1. Default. Pushes changes to the corresponding branch on the upstream repository (the configured remote destination) of the local branch that your current branch is tracking. If the upstream branch doesn't exist yet, you'll need to set up an upstream branch.
git push
  1. "Push changes of main to origin"
git push origin main

main - local branch whose contents should be pushed to the remote repository

  1. Same as [2.] but also sets the upstream branch to use git pull going forward, without specifying any arguments (-u or --set-upstream).
git push -u origin main

-u - required for the first time you're pushing a new branch. After that git push for all future pushes.

  1. "Not your repo but still want to push".
git push --set-upstream <repo-name> Changes

--set-upstream - sets up the current branch to track a remote branch (which was created by someone else) Changes - refers to whatever changes you want to push

About

Git command cheat sheet that serves as a quick reference for essential commands such as navigating repositories, managing commits, and streamlining collaboration with branches and pushes.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages