GitHub - reset local branch and pull new changes - bash

I cloned a repository and checked out specific branch and start editing one file.
My friend also cloned and checked out the same branch and start editing the same file.
We then troubleshoot together on his computer and confirm that everything is working.
He then pushed all changes to the same branch.
How do I properly reset/cancel all my changes (of the same branch) and just pull new ones?
(the ones that he pushed)

You can first discard all your changes
git reset --hard
Then you can pull his version of the branch that he pushed
git pull

How do I properly reset/cancel all my changes and just pull new ones?
(the ones that he pushed)
There are few options:
Delete the local branch and check it out again:
# delete your local changes
git branch -D <branch name>
# fetch the changes or checkout the branch
git fetch --all --prune
# now checkout out the desired branch again
git checkout <branch>
Reset to any given point
# "remove" all your changes from the tip of the branch
# see the next section on how to get the required SHA-1
git reset <SHA-1> --hard
# grab the changes from the remote
git pull origin <branch>
How to find the last common commit?
# Get latest changes from the remote
git fetch --all --prune
# find the last "shared" commit of your 2 branches
git merge-base <branch> origin/<branch>
Check if your branches are up to date
# show remotes and local branches and they status against the remote
git remote show origin

Related

git clone "you appear to have cloned an empty repository" but it's not empty, pull says "couldn't find remote ref master"

I'm trying to set up a vanilla new separate repository for the first time with git 2.37.1 on two W10 systems using drive mapping but I can't find any answered questions that fit my case given my novicitry. :-) Sorry the format is messed up, I did 4 spaces in front of code but most code lines ignore that for some reason.
Spent hours looking at other similar questions, but they all seem to say that there are no commits in the remote repository I'm trying to clone, which is not true. There are lots of commits.
I created a git repository on System1 and it works.
git ls-tree --full-tree --name-only -r HEAD
shows all my files (with lots of commits)
git show HEAD
shows
commit 557...27d (HEAD -> master)
# and the diff between the latest and previous commit
And
git branch -a
shows
* master
No branches, no complexity, no nothing
I go to System2, and create my repo directory, cd into it, then
git init
git remote add origin s:/cap2office # that's my mapped System1 repository)
git pull origin master
I get: fatal: couldn't find remote ref master
git remote
I get: origin
git branch -a
shows nothing
git ls-remote
shows: From s:/cap2office
I also tried:
git clone s:/cap2office
and it says
Cloning into 'cap2office'...
warning: You appear to have cloned an empty repository.
I know I'm missing some trivial magic command, but can't figure out what it is.
You are not cloning a repository, you are trying to clone the working directory of a git clone.
The git repository is stored in the .git folder inside the working directory.
Try this:
git remote add origin s:/cap2office/.git
On system 2, instead of doing git init then git remote add and git pull, do just this:
git clone s:/cap2office/.git
or if you're in Git Bash, sometimes the colon does not work so you need
git clone s/cap2office/.git
Then you can cd to the folder where it was cloned and do the usual git status, git checkout, change stuff, commit, and push to the remote.

The local repository is out of date when push the code even after pull is completed

1.Pull the latest code[
2.Commit the new code
3. When tried to push the code to bitbucket repository getting message "Local repository out of date[![enter image description here]
**
tried bothway Xcode push and cmd based push but no luck still getting this weird message
Note:
Xcode 12.4,
bit bucket
I don't understand, the image shows after the "pull"? If not, please edit your question and place the image in the correct place.
That message can be because someone else have pushed to your repo after the last time you pulled from it. You will need to revert all the X commits that you have done after your last succed push (save the work in another branch or whatever):
git reset --hard HEAD~X
Or reset your local branch to directly match your remote branch:
git reset --hard origin/remote-branch-name
Pull from the repo, make your commits and then you will be able to push again.
You have another option: PULL FORCE!!!
If you alredy known the "push --force" command, then pull force will sound you familiar, but in reality doesn't exist something like "pull --force", but exist a way to replicate that functionality in pull requests:
git fetch --all
git reset --hard origin/master
git pull origin master
(Replace "master" by your branch name)
With that command you overwrite your git history to match exactly the history of your remote repository branch. That will overwrite local changes (the ones that you have not pushed yet) and then you can make your commits and push them.
You can avoid this ackward situation by always working in a new branch only for your work, and when you need to integrate your changes you can make rebase or merge.
There are changes in your local repository that are not on the remote and vice versa, causing a conflict in your history.
You could try the following in your local repository (assuming your remote is called origin):
Make sure your local changes are committed (git commit -am "Some commit message")
Create a new branch (git branch new-branch-name)
Fetch the remote branch (git fetch origin)
Reset the target branch to the version on the remote (git reset --hard origin/target-branch-name)
Merge your newly created branch (git merge new-branch-name)
Push your changes to the remote (git push origin target-branch-name)
Finally resolved it by push the commits to the new PR and then merged the code with old PR

Unable to update my GitHub from my Mac

Why am I unable to update my GitHub from my Mac?
git status produces the following:
On branch master Changes to be committed: (use "git reset HEAD
<file>..." to unstage)
new file: file.test
Untracked files: (use "git add <file>..." to include in what will be
committed)
.DS_Store
git push produces the following;
fatal: The current branch master has no upstream branch. To push the
current branch and set the remote as upstream, use
git push --set-upstream origin master
You need to git add ., then git commit -m "My commit message", then git push -u origin master.
That will set the upstream to the origin/master. Moving forward, just git push shall suffice
Git works through commits - which are basically snapshots of what your repository looked like at a given point in time. You need to commit the files to your repository before you can actually push them to GitHub.
So your first step is to add the files to the "Staging Area" with
git add file.text # you can do git add . but this will add all files which you may not always want
Now you can check the current status of the "Staging Area" with
git status
This will let you make sure that you are only committing the changes that you want added.
Now you can commit the changes. Committing will only 'save' the files that are in the Staging Area.
git commit -m "A useful description of what you did since your last commit"
Ok so now you're ready to push. Assuming you cloned from GitHub you can just run
git push origin master
But if you created this repository with git init you will need to tell git that you have a remote repository somewhere. Do this by running
git remote add https://github.com/<usernane>/<repo_name> origin
This origin is the name you would like to associate with the remote repository. 'Origin" is the most common but you may have other remotes like "backup" or "code_review" for different use cases.
Once you have added the remote repo, you can actually push to it with
git push origin master
Again, origin is the name for your remote repo and 'master' is a 'branch' name.
You can add the -u flag which will make it so that git assumes you want to push to origin. So in the future you would only need to run
git push
Listen to the error messages. :)
You currently have one untracked file, .DS_Store which I happen to know is a system file, so you may want to add that to your .gitignore
As for trying to push upstream, you need to set your upstream for each branch, so you simply need to type the command
$ git push --set-upstream origin master
then do a simple
$ git push
to send your changes to github.
If the branch you are pushing to online is ahead of you, then you may need to do a git pull to get grab the changes, before you do a git push.

Why do git commits exist on my local clone/directory but not on GitHub?

I have committed and pushed several changes for a project using git on the command line, but when I log in to GitHub, the branches and commits I made are not shown on my account. Why?
I can review all commit history I have made using git on the command line.
Results of git remote -v command
origin https://github.com/felixtan/guessing-game.git (fetch)
origin https://github.com/felixtan/guessing-game.git (push)
Once committed locally, you still need to push those commits to github:
git push
(since your remote is named origin, you don't need to specify its name: it pushes by default to 'origin')
This assumes you are the owner or one of the collaborators of the repo felixtan/guessing-game.
If it is the first time you push your current branch:
git push -u origin yourCurrentBranch
That establish a tracking relationship between your branch and 'origin/yourBranch', as detailed in "Why do I need to explicitly push a new branch?".
Once that first push is done, the subsequent pushes are simple 'git push'.
If you are not the owner/collaborator, you won't have the right to push to that repo.
You need to make a fork (See GitHub forking), and in your local cloned repo you are already working on:
git remote rename origin upstream
git remote add origin https://YourLogin#github.com/YourLogin/guessing-game.git
That way, you will push to your fork (that you own), and will make pull requests from there (See GitHub pull requests).

Git revert local commit

I have a git repo hooked up in phpstorm on windows. I committed a few change sets then pushed them to our "central repo". After this I have then made a few more commits. I no longer want these commits that have not been pushed to the central repo. How do I clean my working copy to be the same as the central repo (origin)?
git reset --hard remotes/origin/HEAD
git reset --hard remotes/origin/YOUR/BRANCH
better than /HEAD because you won't see this:
$ git status
On branch MY/BRANCH
Your branch and 'origin/MY/BRANCH' have diverged,
and have 1 and 1 different commit each, respectively.
You can revert local commit by
git reset HEAD~N
where N is used to revert number of commits. An example:
if you have to revert single commit from local, then you can use
git reset HEAD~1
or
git reset HEAD^
If you feel sure about that and don't have any local uncommitted changes:
git reset --hard origin/master
where origin/master is the branch you had pushed to.
The ref-log will still contain the reverted bits, until a garbage collect expires them. To revert the revert,
git reset --hard HEAD#{1}
As per my understading, you create some commit that you have pushed on central repo, after that you have create some more commit, but these are exist on local. These all did not push on central repo.
To remove/revert local commit;
git reset HEAD~{number_of_commit}
simply, you hit git log on your command prompt and get list of commits. Have a look, how many commit you have created now and how many you have to revert.
for example, you have to remove.revert your two last commit then hit
git reset HEAD~2
There is one another way to reset your local repo with central repo. But, in this case your local commit will be removed and if other users push commit on central repo then your repo will be updated with that.
command is :
git reset --hard remotes/origin/HEAD

Resources