Skip to content

Instantly share code, notes, and snippets.

@miPlodder
Last active July 11, 2022 06:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miPlodder/f964112753b560960b51bb0c584d4870 to your computer and use it in GitHub Desktop.
Save miPlodder/f964112753b560960b51bb0c584d4870 to your computer and use it in GitHub Desktop.
Git Commands Cheatsheet

Basic Commands

  • Initialising a Repository
>> git init 
  • Adding changes to ready to commit state
# adding selected files
>> git add filename1 filename2

# adding all files
>> git add . 
  • Check status of Repository
>> git status
  • Commiting Staged Changes
>> git commit -m "write your message here"
  • Checking previous Commits
# show all the commits
>> git log

# shows specific number of commits with lesser details
>> git log --oneline -3
  • Adding Author's details
>> git config --global user.name "name"
>> git config --global user.email "anyemail@gmail.com"
  • Cherry-pick (moving one commit from another branch to current branch using COMMIT-ID)
>> git cherry-pick <COMMIT-ID>

Working with Branches

  • Creating a new branch
>> git branch <branchname>
  • Show which is current branch
>> git branch 
  • Show all branches in the project
>> git branch -a
  • Move to another branch
>> git checkout <branchname>
  • Create new branch and move to it
>> git checkout -b <newbranchname>
  • Deleting a branch locally
>> git branch -d <branchname>

# force delete 
>> git branch -D <branchname>
  • Renaming a branch
>> git branch -m <oldname> <newname>

Working with Remote and Github

  • Creating a remote
>> git remote add <remotename> https://github.com/miPlodder/Project-Name-Link-From-Github
  • Pushing changes to Remote
>> git push <remotename> <branchname>
  • Pulling changes from Remote
>> git pull <remotename> <branchname>
  • Force Push to Remote
>> git push -f <remotename> <branchname>
  • Deleting a remote branch
>> git push <remotename> :<branchname> 

Hacking around unwanted changes

  • Delete last commit
>> git reset --hard HEAD~1
  • Adding changes to last commit (instead of creating a new commit)
>> git add .
>> git commit --amend
  • Deleting modified file changes
>> git reset --hard 
  • Deleting untracked files
# shows which untracked files are about to get deleted
>> git clean -n 
# delete the untracked files 
>> git clean -f
  • Updating last commit message
# this will make changes on local repo
>> git commit --amend

# now if you want to make changes to your remote repo (force push)
>> git push -f origin master

Squashing Last 'n' Commits

* Command to squash last 'n' commits from HEAD
>> git rebase -i HEAD~3

Fetching Remote ORIGIN branch and creating to Local branch

>> git fetch <remote> <rbranch>:<lbranch>
>> git checkout <lbranch>

Reverting last n commits

git revert --no-commit HEAD~n..
git commit -m "your message regarding reverting the multiple commits"

The .. helps create a range. Meaning HEAD~3.. is the same as HEAD~3..HEAD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment