Skip to content

Instantly share code, notes, and snippets.

@vprasadreddy
Created October 11, 2024 16:22
Show Gist options
  • Select an option

  • Save vprasadreddy/41df8a938fe3e73c322443eec53cf962 to your computer and use it in GitHub Desktop.

Select an option

Save vprasadreddy/41df8a938fe3e73c322443eec53cf962 to your computer and use it in GitHub Desktop.
git commands
## To configure git for a project in VS Code
1. git remote add origin repo_url
2. git push origin [branch name]
git remote -v to see current origin remote url
//to change existing origin url
git remote set-url origin new.git.url/here
//to see remote repo associated with current project
git remote show origin
git add .
git commit -m "enter message"
git push origin master
git push origin master --force (not recommended)
or git push <git url>
/* To connect to remote git repository from local project folder. */
git init .
git remote add origin <git url>
/*
to add or push any files from local project folder to remote git repository
***References used***
https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository
*/
git status
git add .
git commit -m "Initial commit"
/* before pushing we need to pull files(if any) from remote git repository to local project folder*/
git pull origin master
/* sometimes "git pull origin master" command gives an error "fatal: refusing to merge unrelated histories". Here are the link for error reference
https://www.mkyong.com/git/git-pull-refusing-to-merge-unrelated-histories/
https://www.educative.io/edpresso/the-fatal-refusing-to-merge-unrelated-histories-git-error
https://github.community/t5/How-to-use-Git-and-GitHub/How-to-deal-with-quot-refusing-to-merge-unrelated-histories-quot/td-p/12619
*/
/* In that case run the below command*/
git pull origin master --allow-unrelated-histories
/* After content is successfully pulled from remote git repository to local project folder. Run the below command.*/
git push origin master
//To see all branches
git branch
//to change branch
git checkout master
or
git switch master
//to create a new branch and switch to new branch
git checkout -b master
//To remove cache or previous .gitignore history
git rm -rf --cached .
//To see ignored files
git status --ignored
//to remove remote git
git remote remove origin
//to remove heroku git from a project
git remote rm heroku
// to see git global username & email
git config --edit --global
//to config default branch at global level
git config --global init.defaultBranch master
//To remove git initialization in a directory
rm -force .git
git remote rm origin
git rm -rf .git*
// to delete initialized git using git init
git rm -rf .git
//To append changes to last commit
git commit --amend --no-edit
git push -f
//to abort git pull origin changes
git merge --abort
git squash to remove unwanted commits messages, but retain the changes.
- Clean history
git rebase
git merge fast-forward
git merge vs rebase
//to revert a commit
git revert 882ad02
git reset is a good option when you realize that the changes being made to a particular local branch should be somewhere else. You can reset and move to the desired branch without losing your file changes.
git revert is a good option for reverting changes pushed to a remote repository. Since this command creates a new commit, you can safely get rid of your mistakes without rearranging the commit history for everyone else
git stash
git stash list
git stash pop
git stash drop
//to clone repos from Azure DevOps
https://username@dev.azure.com/organization_name/Agile_Project/_git/TestRepo
git@ssh.dev.azure.com:v3/organization_name/Agile_Project/TestRepo
//to check the diff between local repo and stages changes
git diff --staged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment