Understanding git init

后端 未结 5 1390
情书的邮戳
情书的邮戳 2021-01-30 14:57

What is git init for exactly? Must I do it once per computer or once per project that uses git? I downloaded my project by git clone and got it working

相关标签:
5条回答
  • 2021-01-30 15:06

    That's actually a lot of questions and misunderstandings. I'm not sure I'd be able to address them all so I'm only going to address what's directly asked.

    1. git init is for every project

      Almost correct. git init is used to start using git on a project that's not under git. For projects that are already under git you use git clone.

    2. The git folder under "Users" is local repo

      Almost correct. The folder is actually .git not git. This comes from the unix convention that all files and folders that start with a dot are considered hidden.

      Secondly, the folder is not under your Users folder. It is under your project folder. So the folder C:/Users/myUser/ is one project. If this is not your intention then you most likely have accidentally executed git init in your User folder.

      Each project has one .git folder in the project's root directory and that is the project's repository. This is one of the reasons git is so fast compared to svn or cvs - the entire repository is processed on the local hard disk without any network traffic.

    3. When I do git push , it takes from that local repo, and puts to remote

      Correct, but only for repos that have remotes (which are usually repos that you create by using git clone to copy a remote repo).

      Note that the remote repo does not need to be on another machine. You can git clone a project from a local folder into another folder and then you can push changes from the new folder back to the original folder.

      The git clone command automatically sets up the necessary config for your repo to connect back to a remote. But you can also manually configure a repo set up with git init to connect to a remote.

    4. what "manages" it

      The .git folder manages your project's repo. Git doesn't run as a server*. Instead the .git folder acts as your local 'server' that all the git commands communicate with. Basically, running a git command edits the contents of the .git folder.

      *note: Remote repos do run servers so you can connect to them. But technically they're not really git servers. They're file servers that git can download from and upload to.

    0 讨论(0)
  • 2021-01-30 15:12

    @Jaanus just one addition to what @slebetman explained regarding git pull. It's not exactly syncing but rather fetching the commits which are not on your local. This is more of a corner case, consider the following -

    1. Assuming to be dealing with a branch test_branch. A, B and C commits exist for origin/test_branch (the branch on your git server) where C is the most recent commit. You have taken a pull and now have A, B and C on your local branch too.

    2. Let's say for some reason you had to reset the commit B and force re-write history of origin/test_branch leaving the history to be A and C.

    3. Now when you perform git pull onto your local. It is going to say everything already up to date but notice you have additional changes of commit B. Therefore, do not consider this as a sync operation but more of a get what I don't have operation.

    Hope that was helpful.

    0 讨论(0)
  • 2021-01-30 15:13

    Your three four understandings are more or less correct, though you're missing a few details.

    git init initialises (i.e. creates) a repository. Each project should be in its own repository.

    If you downloaded your project using git clone then you don't need to run git init again.

    You should be able to copy your project to another directory without any adverse effects. That path was probably chosen by default. Be sure to move the whole directory. The metadata that git needs to run is stored in hidden files in the project's directory.

    Pushing and pulling can get complicated, especially when you're working on a project with others, and when you're using branches. It's not really sensible for me to write out a complete intro to the topic here, so I'd suggest you go read Pro Git for a more thorough explanation.

    0 讨论(0)
  • 2021-01-30 15:19
    #!bin/bash
    DATE=`date +%m%d%Y.%H%M%S`
    TARGET="$1.$DATE"
    DIR=`pwd`
    
    
    function batch_convert() {
    for file in `ls $1`
    do
    if [ -d $1"/"$file ]
    then
    batch_convert $1"/"$file
    else
    dos2unix $1"/"$file
    #echo $1"/"$file
    fi
    done
    }
    
    ##################
    
    echo $TARGET
    cd $DIR/DCW
    git pull
    cd ../
    batch_convert DCW
    cp -R DCW tmp/$TARGET
    cd tmp/$TARGET
    find . -type d -name "*git*"| xargs -n20 rm -rf
    
    
    
    for db in `cat $DIR/tmp/$TARGET/Dblist` ; do
            echo "********** DB IS $db *******"
           for dbfolder in `find * -maxdepth 0 -type d` ;do
        echo `ls -a`
            echo "***** DBFolder is  $dbfolder *****"
        if [ ! $dbfolder = $db ];then
        cp -R $dbfolder $db
        find $db -name "*.ctl"| xargs -I '{}' mv '{}' $db/${db}.ctl
        fi
        done
    done
    cd ../
    tar -cf $TARGET.tar $TARGET
    
    0 讨论(0)
  • 2021-01-30 15:23

    git init is only for when you create your own new repository from scratch. It turns a directory into an empty git repository.

    When you use git clone to clone an existing repository into a new one, git init is neither necessary nor desired.

    0 讨论(0)
提交回复
热议问题