0

In my linux machine, I have cloned the git repository created 2 new files and am trying to push the newly created branch into git with the new file created. When I run the code branch is getting created and when I go inside the branch I am seeing some other branch files. Please find below code

Step Followed:

1.git pull

  1. git create branch

  2. make file changes

4.git commit

5.git push

Code:
#!/bin/bash

git config --global user.name ""
git config --global user.email ""
git clone repo name
cd tf-infra
git checkout -b testbranch
git status
git symbolic-ref --short HEAD
git rev-parse master
git rev-parse --symbolic-full-name master@{u}
git remote --verbose

#code

git commit
git push orgin testbranch

When I run the code, new branch(test branch) is created in git. but when I go into the test branch I can see some other branch files inside my branch. Basically the files which I create are not pushing into git. When I run the code new branch is created and inside the branch it is showing some other files(existing branch files). Please help me to understand where I am going wrong

Kindly help!

6
  • 2
    Are you doing git add?
    – ParthS007
    Commented Mar 24, 2020 at 7:45
  • @ParthS007 I am using git add. I am just cloning the existing git repo in my Linux machine
    – disha
    Commented Mar 24, 2020 at 7:59
  • What do you mean by cloning existing git repo?
    – ParthS007
    Commented Mar 24, 2020 at 8:06
  • We have a repository in a GitLab. For new deployment, I need to create files. I am trying to automate it. For this purpose, I am cloning the existing rep from git in my linux machine and adding those file through code and again push it to git in a test branch
    – disha
    Commented Mar 24, 2020 at 8:10
  • @disha: Assuming that you start with an empty directory, git clone ....; git checkout -b ..... will not suddenly show up files which had not been in the original repo. Someone must have checked in them before, or your directory name already existed when you did the clone . Commented Mar 24, 2020 at 8:22

3 Answers 3

2

When you create a branch, the branch is not empty, your new branch starts right from where you created the branch - it continuous your work - it literally "branches". That's why it is named "branch".

When you clone a repository (git clone), your working copy automatically checks out the current HEAD which is most likely the "master" branch.

If this "master" branch has file foo and file bar, and when you then create a new branch and add a new file foobar (with git add and git commit and git push) than your branch will have three files: foo, bar and foobar.

1
  • yes, "git checkout -b testbranch" from this command new branch will start. In my case Initially i clone the repo and i am creating the new branch and making the changes in the new branch and pushing it. In the git UI i can see the newly created branch, but when i go inside the branch, the branch shows some other files(some existing branch files) Could you please help me
    – disha
    Commented Mar 24, 2020 at 8:01
0

To commit files that are newly created or you have made some changes, you have to stage the files before creating a commit.

You can do like this:

  1. git status

    Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by git (and are not ignored by gitignore[5]). The first is what you would commit by running git commit; the second and third are what you could commit by running git add before running git commit. Source

  2. git add

    This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit. It typically adds the current content of existing paths as a whole, but with some options, it can also be used to add content with only part of the changes made to the working tree files applied or remove paths that do not exist in the working tree anymore.

  3. git commit -m <"Message">

  4. git push origin testbranch

5
  • If i add git status, git add, git commit -m <"Message">,git push origin test branch in my code, the new branch which is created will contain my files. It won;t show some other branch file right? I did not add 1 & 2 cmd. Is that a reason my branch was showing some other branch files
    – disha
    Commented Mar 24, 2020 at 8:13
  • I can't say but other branch files will not show for sure and following this way, you will be able to do what you are expecting i.e new files are added.
    – ParthS007
    Commented Mar 24, 2020 at 8:14
  • thank u i will try this. When i run the command " git remote add origin github.com/xxx/yyy.git" am getting fatal: Not a git repository (or any parent up to mount point /home) Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set). What is the reason
    – disha
    Commented Mar 24, 2020 at 8:23
  • You are not into the git repo that is what error is saying.
    – ParthS007
    Commented Mar 24, 2020 at 8:25
  • Let us continue this discussion in chat.
    – disha
    Commented Mar 24, 2020 at 10:57
0

Git doesn't store files. Git stores commits.

Branches don't have files; branches have commits.

When you push from one Git to another, you don't push files. You push commits.

This might seem like a lot of repetition, but that's the point: you need to keep in your head that Git deals in commits. So: what exactly is a commit?

Well, internally, it consists of two parts:

  • There's a snapshot. This has files! Yay, finally we have some files. 😀

    There is something peculiar about these files, though. They're not ordinary files. You can't work with them. They're frozen for all time, and compressed, and turned into a format that only Git can read. You can't access them or change them. They're no good for anything except archival. So we're going to need a way to get them out of Git, which we'll get to in a moment.

  • And, there's some metadata in each commit, such as the name and email address of the user who makes the commit.

    This is what your git config --global user.name is for. You should not set this to the empty string, and you should not set your email address to the empty string either. Git doesn't actually verify that what you set here is valid, but as good practice it's a good idea to use something real.

Every commit gets a unique hash ID at the time it gets created. From then on, that hash ID means that commit, and no other; that hash ID is forever reserved for that commit. You generally won't deal directly with hash IDs, except for certain occasions where you might use git log and then cut-and-paste one into a git revert or git reset command, once you get to that point. But remember that the hash ID is how Git knows which commit is which. Branch names like master or testbranch serve to remember one hash ID (the hash ID that a name remembers changes over time).

To create a new branch, you ask Git to make a new branch, using some existing commit hash ID. The default hash ID is the current commit. There is always a current commit,1 and almost always a current branch name.2 When you use git checkout master, you're asking Git to select the branch master and therefore the last commit in that branch.

When you ask Git to create the new branch name testbranch using git checkout -b testbranch, you're asking Git to make the new name hold the same commit hash ID, and select that name as the current branch, and therefore that commit hash ID as the current commit. So Git doesn't change commits, but it does change branch names. Both branch names identify the same commit.

Now, let's get on to how you work with files, instead of a whole commit.


1There is an exception or two to this rule, including in a new totally-empty repository where there are no commits, but we won't deal with that here.

2The case when there's no current branch is what Git calls a detached HEAD. We won't deal with that here either.


Dealing with files

Fundamentally, each Git commit is read-only and totally unchangeable and its files are in a useless format. To make Git useful we must have it extract those files and turn them back into the computer's everyday format.

This is what git checkout (or git switch in Git 2.23 and later) does: you select a branch name, such as master, and thereby a specific commit hash ID, and you ask Git to please copy the commit's files to a work area.

This work area, where files have their ordinary everyday form, is your working tree or work-tree. The two names refer to the same thing. Note that Git itself does not use this work-tree, other than to fill it in from a commit. This work-tree is for you to use.

You now use it. One of the things you can do with it is create new files. Another thing you can do is open up some existing file in an editor, edit it, and write the file back. Last, you can remove files entirely. All of these actions happen only in your work-tree. The changes you have made to your work-tree are not in any commit.

You will eventually have to put them into some commit(s). To do this you will need to use the command git commit. You might assume that this would look at your work-tree and use the files that are there to make the new commit. That would be sensible but that is not what Git does.

Instead, Git builds a new commit from a third copy of each file. This third copy is found in Git's index.3 If you have modified a file in the work-tree, or added a new one, you must use git add to copy the updated or new work-tree file into the index.


3Technically, the index contains a reference to the file's content, rather than an actual copy. However, until you start exploring git ls-files --stage and git update-index, you can just think of the index as holding a copy of the file.


Making a new commit from the index

To make the new commit, you run:

git commit

(or maybe git commit -m <message>: if you leave out the -m, your Git will open an editor for you to type in the message). This creates a new snapshot with metadata based on your git config settings and what you set as your log message, explaining why you made this commit.

Git makes this new commit from the files that are in the index, with whatever data they have in as stored in the index. So the new commit you just made will now match the index. The new commit is now the current commit, and Git writes the new commit's hash ID—which is unique to this commit—into the current branch name.

Note how the initial git checkout filled in Git's index from the commit being checked out, and how this new git commit made the commit from whatever was in Git's index. So as a sort of general rule—with exceptions, again—the index tends to match the commit right after a git checkout or git switch, and also to match the commit right after a git commit.4

So it's your changes to your work-tree in between that make things happen, but once you have changed your work-tree, you must use git add to copy the updated files back into the index. Any new files need to be git add-ed, which seems clear enough to new users of Git, but changes to existing files need to be git add-ed as well, which is a bit of a surprise.5


4The exceptions to this rule get very complicated. See Checkout another branch when there are uncommitted changes on the current branch.

5Git does offer git commit -a, which tells Git: for all files that are already in the index, do a git add on them as needed, then do the git commit. This is almost the equivalent of running git add -u before committing, except for certain weird corner cases that can only be explained by noting that Git also allows for additional temporary index files, and this kind of git commit uses them.


The index is also called the staging area: about git status

The term index and staging area both refer to the same thing. While you can't see the index directly,6 you can have Git tell you about the index / staging area.

Remember, there are three active copies of every file: the one in the current commit—this one is frozen for all time—plus the one in the index plus the one in your work-tree. The git status command helps you out with these three copies by doing two separate comparisons.

The first comparison compares every file in the current commit with every file in the index. When those two files match, git status says nothing. When they are different, git status says that this file is staged for commit.

The second comparison compares every file in the index with every file in your work-tree. When those two files match, git status says nothing. When they are different, git status says that this file is not staged for commit.

So, if file F in your work-tree is not staged for commit, running git add will copy the contents of file F into the index copy of file F. Now the index copy of F won't match the committed copy, and file F will now be staged for commit. If you change the work-tree copy again, the file will be both: it will be not-staged and staged, at the same time. This is possible because there are three copies of F.


6Technically, you can: use git ls-files --stage to see it. Note that this command is verbose and not user-friendly even by Git standards.


Untracked files

Because your work-tree is yours, you can put files into it that are not in Git's index. When you do this, git status will notice that there is no index copy of the file. It will say that this file is untracked.

That is all that an untracked file is: it's a file that exists in your work-tree, but not in Git's index. Because it is not in Git's index, it will not be in the next commit.7 You will need to use git add to copy it into Git's index, so that it is now staged for commit: the index copy, which now exists, is different from the (nonexistent) current-commit copy.


7Note that git commit -a won't add this file: it only runs git add on files that are in the index.


Ignored files

The git status command is extremely useful. Use it often. Note, however, that every time you use it, it whines about your untracked files. If you have a lot of untracked files, you will get a lot of complaints from git status.

You can list untracked files in a file named .gitignore. If you do so, git status will stop whining about them. The git add command will also stop adding them to Git's index. So, by listing files that are currently untracked, and that you don't want to have tracked, you can get Git to automatically not commit them and stop whining about them.

Listing a tracked file in a .gitignore has no effect. So .gitignore is not really files to ignore; instead, it's files to not complain about being untracked and not automatically add with an add-all-files operation such as git add *. But calling this file .git-do-not-complain-about-these-untracked-files-and-do-not-add-them-when-I-use-an-en-masse-add-command would be unwieldy, so it is called .gitignore.

Why cloning gets you a work-tree

When you run:

git clone <url>

your Git will:

  1. create a new empty directory;
  2. inside that directory, run git init to create an empty repository: one with no commits at all;
  3. use git remote add to add a remote to that empty repository: this remote is normally named origin;
  4. do any other configuration needed inside this new empty repository;
  5. run git fetch origin to obtain the commits from the other Git, and then rename their branches, such as master, to your own Git's remote-tracking names such as origin/master;
  6. finally, run git checkout on some branch name, with the usual default here being master.

This last step, the git checkout master, creates your master branch from your remote-tracking name origin/master—which holds a commit hash ID, just like a branch name would, since it's copied from their Git's master branch—and then selects that as the current branch and current commit. It's this checkout step that fills in Git's index and your work-tree. So that's why you see actual files: your Git has extracted them, from the commit that is the last commit in your master branch. Your Git just created your master branch to match the other Git's master branch, which your Git is now remembering as your origin/master.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.