132 lines
No EOL
3.8 KiB
Markdown
132 lines
No EOL
3.8 KiB
Markdown
# Git commands
|
||
|
||
- Resetting local to specific commit
|
||
|
||
[https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit)
|
||
|
||
`git reset --hard <commidId> && git clean -f`
|
||
|
||
or a safer
|
||
|
||
`git clean -i`
|
||
|
||
commit id:
|
||
|
||

|
||
|
||
Use `git log` for commit id.
|
||
|
||
### Reverting Working Copy to Most Recent Commit
|
||
|
||
To revert to the previous commit, ignoring any changes:
|
||
|
||
```bash
|
||
git reset --hard HEAD
|
||
```
|
||
|
||
where HEAD is the last commit in your current branch
|
||
|
||
### Reverting The Working Copy to an Older Commit
|
||
|
||
To revert to a commit that's older than the most recent commit:
|
||
|
||
```bash
|
||
# Resets index to former commit; replace '56e05fced' with your commit code
|
||
git reset 56e05fced
|
||
|
||
# Moves pointer back to previous HEAD
|
||
git reset --soft HEAD@{1}
|
||
|
||
git commit -m "Revert to 56e05fced"
|
||
|
||
# Updates working copy to reflect the new commit
|
||
git reset --hard
|
||
|
||
# Push your changes to respective branch
|
||
git push -f
|
||
```
|
||
|
||
save modifications before resetting:
|
||
|
||
```bash
|
||
# Alternatively, if there's work to keep:
|
||
git stash
|
||
git reset --hard 0d1d7fc32
|
||
git stash pop
|
||
# This saves the modifications, then reapplies that patch after resetting.
|
||
# You could get merge conflicts, if you've modified things which were
|
||
# changed since the commit you reset to.
|
||
```
|
||
|
||
To revert changes after git push use revert instead of reset —hard
|
||
|
||
`git revert <commit_hash>`
|
||
|
||
or simply use below after using reset —hard
|
||
|
||
`git push -f origin main`
|
||
|
||
do not use —force, this will make all your coworkers hate you, use —force-with-lease, this will only allow you to push the code if there are no conflicting changes with the current parent branch
|
||
|
||
- Branches
|
||
|
||
[https://www.geeksforgeeks.org/how-to-set-upstream-branch-on-git/](https://www.geeksforgeeks.org/how-to-set-upstream-branch-on-git/)
|
||
|
||

|
||
|
||

|
||
for adding remote url
|
||
`git remote add <name> <url>`
|
||
then
|
||
`git push --set-upstream <name> master`
|
||
PS C:\Users\harsh\Videos\imp\react\github\test1> `git branch main -u origin/main`
|
||
Branch 'main' set up to track remote branch 'main' from 'origin'.*
|
||
|
||
*PS C:\Users\harsh\Videos\imp\react\github\test1> `git branch test -u origin/test`
|
||
Branch 'test' set up to track remote branch 'test' from 'origin'.
|
||
|
||
`git push --set-upstream origin <branch name>`
|
||
to show remote git details like url and all that
|
||
`git remote show origin` or `git remote -v` (to see only url)
|
||
To edit current url
|
||
`git remote set-url origin git@bitbucket.org:tutorials/tutorials.git`
|
||
|
||
### **Merging other branch to main:**
|
||
|
||
Make sure first you update your **development** branch with master to resolve conflicts issue(if there are any):
|
||
|
||
```bash
|
||
git checkout main
|
||
git pull origin main
|
||
git checkout development
|
||
git merge main
|
||
|
||
```
|
||
|
||
Now see if master branch is merged without any conflict, if there's any conflict then you'll have to resolve them. Once you're done with that, you can:
|
||
|
||
```bash
|
||
git checkout main
|
||
git merge development
|
||
git push origin main
|
||
```
|
||
|
||
To copy content from A to B
|
||
|
||
`git checkout B`
|
||
|
||
`git merge A`
|
||
|
||
If A is test and B is main then
|
||
|
||
`git checkout main`
|
||
|
||
`git merge test`
|
||
|
||
To create a branch of specific commit:
|
||
|
||
`git checkout -b old_state <commit_hash>`
|
||
|
||
- Configuration
|
||
|
||
`git config —-global [user.name](http://user.name) 'moss'` |