Note: If you do not create a 'read me' file in Github, then there will be no 'innitial commit' in the remote origin.
$ mkdir git-pull-test-1
$ cd git-pull-test-1
$ git init
Initialized empty Git repository in /home/kamrul/Projects/git-pull-test-1/.git/
$ git remote add origin [email protected]:kamrulcodes/git-pull-test-1.git
$ touch 'A file to stage.txt'
$ git add 'A file to stage.txt'
$ git commit -m 'Initial Commit.'
[main (root-commit) 30891c3] Initial Commit.
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 A file to stage.txt
$ git push
fatal: The current branch main has no upstream branch. ๐๏ธ
To push the current branch and set the remote as upstream, use
`git push --set-upstream origin main`
$ git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 227 bytes | 227.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:kamrulcodes/git-pull-test-1.git
- [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'. ๐๏ธ
$ git log
commit 30891c38808386912701f49a733c3d9b8b8b7719 (HEAD -> main, origin/main)
Author: Kamrul Hasan ********@gmail.com
Date: Sun Apr 9 03:20:31 2023 +0600
Initial Commit.
$ echo 'Some edit.' >> 'A file to stage.txt'
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: A file to stage.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add 'A file to stage.txt'
$ git commit -m "Edited - 'A file to stage.txt'"
[main b214564] Edited - 'A file to stage.txt'
1 file changed, 2 insertions(+)
Note:
Commit messages should be more specific and in present form.
Like, "Add one line of text in the file 'A file to stage.txt'" or, "fix something in the file ..." etc.
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:kamrulcodes/git-pull-test-1.git
30891c3..b214564 main -> main
$ git log
commit b214564335ff63d5eee0390780c0b7b6264e89cf (HEAD -> main, origin/main)
Author: Kamrul Hasan *******@gmail.com
Date: Sun Apr 9 03:46:42 2023 +0600
Edited - 'A file to stage.txt'
commit 30891c38808386912701f49a733c3d9b8b8b7719
Author: Kamrul Hasan ********@gmail.com
Date: Sun Apr 9 03:20:31 2023 +0600
Initial Commit.
$ git reset HEAD^
Unstaged changes after reset:
M A file to stage.txt
$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: A file to stage.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ echo "Append text to the file">>'A file to stage.txt'
$ git status
$ git add 'A file to stage.txt'
$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch) ๐๏ธ
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: A file to stage.txt
$ echo "Hello World">'A new file - not staged.txt'
$ echo "Hello World">'A new file - staged for the first time.txt'
$ git add 'A new file - staged for the first time.txt'
$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch) ๐๏ธ
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: A file to stage.txt
new file: A new file - staged for the first time.txt
Untracked files:
(use "git add ..." to include in what will be committed)
A new file - not staged.txt
- A new file - not staged.txt is called Untracked file (U).
- A new file - staged for the first time.txt is called NEW FILE (A).
- A file staged and modified multiple times is called modified file (M).
- An untracked file?
- A new file?
- A modified file?
$ git pull
Updating 30891c3..b214564
error: Your local changes to the following files would be overwritten by merge: ๐๏ธ
A file to stage.txt
Please commit your changes or stash them before you merge. ๐๏ธ
Aborting ๐๏ธ
$ git pull --rebase
error: cannot pull with rebase: Your index contains uncommitted changes. ๐๏ธ
error: please commit or stash them.
If there is a modified (M) file exists in the local branch, the 'git pull' aborts.
And asks to stash or, commit before pulling. So, No worries. Local modified (M) files do not affected by a sudden pull request.
It is also not possible to 'pull --rebase' without commit/stash.
$ git commit
[main 156d6ee] One added and one modified.
modified: A file to stage.txt
new file: A new file - staged for the first time.txt
2 files changed, 4 insertions(+)
create mode 100644 A new file - staged for the first time.txt
'git commit' commits both the modified (M) and staged (A) files.
Here, we fail to test the effect of pull on the staged (A) file.
$ git status
On branch main
Your branch and 'origin/main' have diverged, ๐๏ธ
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours) ๐๏ธ
Untracked files:
(use "git add ..." to include in what will be committed)
A new file - not staged.txt
nothing added to commit but untracked files present (use "git add" to track)
'git status' says the the branch is diverged at this point. And it suggests to merge.
$ git push
To github.com:kamrulcodes/git-pull-test-1.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:kamrulcodes/git-pull-test-1.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again. ๐๏ธ
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
If the current branch is behind it's remote counterpart, that means if someone else updates the remote branch, the git push is rejected. So, no worries of unwanted push too.
$ git pull
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge (the default strategy)
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
$ git pull --rebase
Auto-merging A file to stage.txt
CONFLICT (content): Merge conflict in A file to stage.txt ๐๏ธ
error: could not apply 156d6ee... One added and one modified.
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue". ๐๏ธ
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 156d6ee... One added and one modified. modified: A file to stage.txt new file: A new file - staged for the first time.txt
Resolve Conflict:
At this point open files that has 'CONFLICT's in the editor and edit wisely and save. The conflicted files have (!) indications in your code editor.
Then,
$ git rebase --continue
A file to stage.txt: needs merge
You must edit all merge conflicts and then
mark them as resolved using git add ๐๏ธ
$ git status
interactive rebase in progress; onto b214564 ๐๏ธ
Last command done (1 command done):
pick 156d6ee One added and one modified. modified: A file to stage.txt new file: A new file - staged for the first time.txt
No commands remaining.
You are currently rebasing branch 'main' on 'b214564'.
(fix conflicts and then run "git rebase --continue") ๐๏ธ
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch)
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: A new file - staged for the first time.txt
Unmerged paths:
(use "git restore --staged ..." to unstage)
(use "git add ..." to mark resolution) ๐๏ธ
both modified: A file to stage.txt
Untracked files:
(use "git add ..." to include in what will be committed)
A new file - not staged.txt
$ git add 'A file to stage.txt'
$ git status
interactive rebase in progress; onto b214564 ๐๏ธ
Last command done (1 command done):
pick 156d6ee One added and one modified. modified: A file to stage.txt new file: A new file - staged for the first time.txt
No commands remaining.
You are currently rebasing branch 'main' on 'b214564'.
(all conflicts fixed: run "git rebase --continue") ๐๏ธ
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: A file to stage.txt
new file: A new file - staged for the first time.txt
Untracked files:
(use "git add ..." to include in what will be committed)
A new file - not staged.txt
$ git rebase --continue
[detached HEAD 966a109] One added and one modified. modified: A file to stage.txt new file: A new file - staged for the first time.txt
2 files changed, 2 insertions(+)
create mode 100644 A new file - staged for the first time.txt
Successfully rebased and updated refs/heads/main.
The local branch head sits on top of the remote branch in this rebase process.
Note: Must git add <file>
before git rebase --continue
$ git log
commit 966a109ff432a0597405cfd51d236b58e8fb74e5 (HEAD -> main)
Author: Kamrul Hasan ********@gmail.com
Date: Sun Apr 9 04:59:25 2023 +0600
One added and one modified.
modified: A file to stage.txt
new file: A new file - staged for the first time.txt
commit b214564335ff63d5eee0390780c0b7b6264e89cf (origin/main)
Author: Kamrul Hasan ********@gmail.com
Date: Sun Apr 9 03:46:42 2023 +0600
Edited - 'A file to stage.txt'
commit 30891c38808386912701f49a733c3d9b8b8b7719
Author: Kamrul Hasan ********@gmail.com
Date: Sun Apr 9 03:20:31 2023 +0600
Initial Commit.
Pushing is required after this type of pull and rebase. Because the current HEAD is above the remote origin/main.
$ git push
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 443 bytes | 443.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:kamrulcodes/git-pull-test-1.git
b214564..966a109 main -> main
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Untracked files:
(use "git add ..." to include in what will be committed)
A new file - not staged.txt
nothing added to commit but untracked files present (use "git add" to track)
Untracked (U) files remain the same - no matter what.
Now, let's add the untracked file 'A new file - not staged.txt' to test the pull effect on staged (A) files.
$ git add 'A new file - not staged.txt'
$ git status
On branch main
Your branch is up to date with 'origin/main'. ( โ๏ธ can not be correct or reliable status.)
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: A new file - not staged.txt
Untracked files:
(use "git add ..." to include in what will be committed)
A file not to stage.txt
$ git log
commit 526363841305cc6761e14bb5b25f055bfcf566f6 (HEAD -> main, origin/main) ( โ๏ธ NOT Correct. Local HEAD is behind origin/main.)
Author: Kamrul Hasan ********@gmail.com
Date: Sun Apr 9 06:15:25 2023 +0600
Create README.md
So, we can test the pull effect now. Because, we already have new commits (README.md file) in the remote origin/main branch.
'git status' or 'git log' do not confirm the state of the remote origin.
'git fetch' or 'git pull' is essential for that.
$ git pull
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 15 (delta 10), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), 4.28 KiB | 199.00 KiB/s, done.
From github.com:kamrulcodes/git-pull-test-1
5263638..712d5a6 main -> origin/main
Updating 5263638..712d5a6
Fast-forward
README.md | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 59 insertions(+), 19 deletions(-)
'git pull' has no effect on new file (A) what so ever.
Conclution:
- 'git pull' has no effect on local Untracked (U) files.
- 'git pull' has no effect on local New (A) files.
- 'git pull' aborts if there is any Modified (M) file in the local branch.
- 'git status' or 'git log' do not reveal any information about the remote branch.