Do git status
, this will show you what files have changed. Since you stated that you
don't want to keep the changes you can do git checkout -- <file name>
or git reset
--hard
to get rid of the changes.
For the most part, git
will tell you what to do about changes. For example, your error
message said to git stash your changes. This would be if you wanted to keep them. After
pulling, you would then do git stash pop and your changes would be reapplied.
git status
also has how to get rid of changes depending on if the file is staged for
commit or not.
git add -A
or
git add --all
It is a collection of the following two functions "git add .
" and
"git add -u
".
git add .
Monitor the status tree of the work area, and use it to submit all changes
during work to the temporary storage area, including file content modification
(modified) and new files (new), but not including deleted files.
git add -u
or
git add --update
That means:
Only monitor files that have been added (tracked files), and submit the
modified files to the temporary storage area.
Will not submit new files (untracked file)
Or
Update the index just where it already has an entry matching <pathspec>
.
This removes as well as modifies index entries to match the working tree,
but adds no new files.
If no <pathspec>
is given when -u
option is used, all tracked files
in the entire working tree are updated (old versions of Git used to limit
the update to the current directory and its subdirectories).
git rm <file>
git commit -m "delete <file>"
If adding use "git push
", that will delete both local and remote repository files.
Note:
rm <file>
means delete the workspace file.
Notices:
rm -r directory_name_/*
means: delete all the files under the directory_name_.
git branch --delete <name>
As you can see the local branch is deleted.
git log --graph
press key "q
" to Quit.
! [rejected] master -> master (fetch first) error: failed to push some refs to 'git@
or
! [rejected] main -> main (fetch first) error: failed to push some refs to 'git@
or
! [rejected] your_branch -> your_branch (fetch first) error: failed to push some refs to 'git@
This problem occurs because the README.md
file in github is not in the local code directory.
You can use the following command to merge the code:
Firstly
git pull --rebase origin master
or
git pull --rebase origin main
or
git pull --rebase origin your_branch
Then
git push origin master
or
git push origin main
or
git push origin your_branch
git pull --rebase
git pull --rebase
: During execution, each commit in the local
current branch will be cancelled, and then the local current
branch will be updated to the latest "origin" branch.
git pull
== git fetch
+ git merge
;
git pull --rebase
== git fetch
+ git rebase
;
git fetch
: Get the latest version from remote to local, will not
automatically merge branches.
git rebase
: Redefine (re) the role of the starting point (base),
that is, redefine the status of the branch's version library.
If there are merge conflicts after executing git pull --rebase
, use the
following three methods to deal with these conflicts:
git rebase --abort
will abandon the merge and return to the state before
the rebase operation, and the previous commits will not be discarded;
git rebase --skip
will discard the commits that cause conflicts
(use with caution!!);
git rebase --continue
merge conflicts, combined with "git add <file>
"
command to use and repair conflicts, to remind the developer, step by step
if there is any conflict resolution. (Fix conflicts and then run
"git rebase --continue
")
After executing $git pull --rebase
, if there is a conflict locally, after
manually resolving the conflict, use the "git add
" command to update the
index of the content, and then just execute:
$ git rebase --continue
can linearly connect the local branch and the remote
branch, and then exit and return to the main branch after correctness.
Note: In general, there is no problem in checking after modification, use
rebase continue
to merge conflicts.
(1)Discard local conflict file modification after conflict occurs:
git pull
(2)use remote file to overwrite local file:
git checkout [file path]
eg.
git checkout helloworld.cpp
or
git checkout src/main/resources/helloworld.cpp
(1)Update the repository every time before uploading the repositroy, please run
git pull
discover Terminal:
>error: Your local changes to the following files would be overwritten by merge:helloworld.cpp
>Please commit your changes or stash them before you merge.
The error is basically a conflict. This shows that your helloworld.cpp conflicts
with the remote, you need to submit the local modification first and then update it.
(2)Submit the local helloworld.cpp file without pushing remotely
git add helloworld.cpp
git commit -m "conflict fixed"
(3)Update local repository
git pull
discover Terminal:
>Auto-merging helloworld.cpp
>CONFLICT (content): Merge conflict in helloworld.cpp
>Automatic merge failed; fix conflicts and then commit the result.
After the update, (develop|MERGING) will appear on your local branch
similar to this kind of logo.
(4)Delete functions according to prompts
Find your local helloworld.cpp file and open it.
You will find "<<<<<<< HEAD
", "=======
", ">>>>>>> ae9a0f6b7e42fda2ce9b14a21a7a03cfc5344d61
"
in the file.
This kind of mark, "<<<<<<< HEAD
" and "=======
" is your own code,
"=======
" and ">>>>>>>>
" is modified by others Code.
Make sure to keep that part of the code, and finally delete "<<<<<<< HEAD
", "=======
", ">>>>>>
"
this kind of mark, comment out the feature ("ae9a0f6b7e42fda2ce9b14a21a7a03cfc5344d61
").
(5)Submit the local helloworld.cpp file agiain without pushing remotely
git add helloworld.cpp
git commit -m "conflict fixed end"
(6)pushing remotely
git push
(1)After the conflict:
git pull
(2)Temporary local changes:
git stash
(3)Then update the code:
git pull
(4)View the list of temporary records:
git stash list
(5)Release local staging:
git stash apply stash@{0}
Use "git stash apply @{x}
" to release the cache number "x
", but
the cache still exists in the "list
".
(6)Conflicting files appear, find and resolve them:
reference: ####Six step
.
(7)Submit and join the index library:
git add <file>
git commit -m "comment"
(8)Push to remote
git push
(1) Enter the directory of your needed Merging Repository
eg. cd /Users/marryme/GitHub/Personal/Sofija/hello-world-debug-vscode
(2)To view all remote branches
git branch -a
discover Terminal:
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/matMain
remotes/origin/taylorPatch
remotes/origin/DavidPatch
(3)Switch branch
For example, there are three people developing at the same time, matMain
was
originally based on taylorPatch
, but because the project has not been released,
taylorPatch
, DavidPatch
, and matMain
are all developed at the same time. Now I
want to merge the added functions of taylorPatch
to matMain
;
Steps at this time: check taylorPatch
and matMain
:
git checkout taylorPatch
git checkout matMain
Then execute merge based on the branch of matMain
:
git merge taylorPatch
If there is no error, then directly submit the code:
git push origin matMain
If an error is reported, it is basically a conflict(reference:
Fixed conflicts in uploading GitHub files
):
eg:
CONFLICT (content): Merge conflict in src/main/bank_simulate.cc
Auto-merging inc/hand_make_line.h.
CONFLICT (content): Merge conflict in inc/hand_make_line.h
Automatic merge failed; fix conflicts and then commit the result.
You need to go to the prompt file and comment out the version conflict
automatically marked by git, and delete it according to the functions
you need.
Then put the conflicting files git add
, and commit
.
For example, if you have two conflicting files, separate them with spaces
when adding multiple files.
git add src/main/bank_simulate.cc inc/hand_make_line.h
Finally commit:
git commit -m "end the conflict between two branch"
Submit local repository:
git push origin matMain
git branch -a
or
git branch --all
git branch
git branch <name>
git branch (-m) (local branch )<name>
-m
attribute is used to rename the branch name without affecting the branch’s history.
create branch , and switch local branch to the new created branch .
git checkout <name>
git checkout -b <name>
git merge <name>
git branch -d <name>
git fetch
The above command will just fetch all the remote branches to your local.
git push origin --delete <name>
As you can see the old remote is deleted.