Skip to content

Instantly share code, notes, and snippets.

@jamalnasir
Last active July 29, 2021 09:14
Show Gist options
  • Save jamalnasir/29e65999546af1cd399586a73244123e to your computer and use it in GitHub Desktop.
Save jamalnasir/29e65999546af1cd399586a73244123e to your computer and use it in GitHub Desktop.
Git Commands
git commit [some files]
Or if you are sure that you have a clean staging area you can
git add [some files] # add [some files] to staging area
git add [some more files] # add [some more files] to staging area
git commit # commit [some files] and [some more files]
git commit file1 file2 file5 -m "commit message"
If you want to make that commit available on both branches you do
git stash # remove all changes from HEAD and save them somewhere else
git checkout <other-project> # change branches
git cherry-pick <commit-id> # pick a commit from ANY branch and apply it to the current
git checkout <first-project> # change to the other branch
git stash pop # restore all changes again
git stash pop "stash@{0}" # restore specific stash
git stash list # lists all the available stash
git stash drop stash@{n} # deletes a stash
git stash show -p # view the content of the most recent stash
git stash show -p stash@{1} # view the content of specific stash
git stash show -p stash@{0} --name-only # shows just the names of the files (not the contents)
git branch -d branch_name # deletes a branch
git branch -D branch_name # force deletes a branch --delete --force
// RENAME THE LATEST COMMIT MESSAGE
git commit --amend -m "NEW MESSAGE GOES HERE"
git push origin BRANCH_NAME --force
To UNDO local file changes but NOT REMOVE your last commit, then use
git reset --hard
To UNDO local file changes AND REMOVE your last commit, then use
git reset --hard HEAD^
or
git reset --hard HEAD~
To KEEP local file changes and REMOVE ONLY your last commit, then use
git reset --soft HEAD^
or
git reset --soft HEAD~
Use git status and git log frequently to observe your current state.
Reset the working tree to the last commit
git reset --hard HEAD^
look at the output of git log, find the commit id of the commit you want to back up to, and then do this:
git reset --hard <sha1-commit-id>
If you already pushed it, you will need to do a force push to get rid of it...
git push origin HEAD --force
Clean unknown files from the working tree
git clean
This will remove it from the current index (the "about to be committed" list) without changing anything else.
git reset <file>
You can use the following command without any file name to unstage all due changes. This can come in handy when there are too many files to be listed one by one in a reasonable amount of time.
git reset
/**************************************/
/**************************************/
IF THE GITIGNORE FILE DOES NOT WORK PROPERLY. THEN PERFORM THE FOLLOWING STEPS.
1. When you add something into .gitignore file
git rm -r --cached .
git add .
git commit -m "fixed untracked files"
2. When you remove something from .gitignore file.The above steps will not work for you. You can try this:
git add -f "filetype"
git commit -m "Refresh removing filetype from .gitignore file."
the "filetype" means the file or filetype you want to remove from the .gitignore file. You want to make the filetype be tracked again.
/**************************************/
/**************************************/
Remove ^M from git diff:
git config --global core.whitespace cr-at-eol
git config core.autocrlf
This will fix any CRLF to LF when you commit in OSX:
git config --global core.autocrlf input
This will make sure when you checkout in windows, all LF will convert to CRLF:
git config --global core.autocrlf true
https://lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/
/**************************************/
Turning Off Tracking of Executable Bit Change. For file modes change.
git config --local core.fileMode false
Use --global flag instead of --local in the command above if you want it to be used as the default setting for any repos which have not explicitly set it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment