Git checkout commit, do stuff, and go back to master

I have a build system that takes a git repository, goes back to a specific commit, and then uploads those files some where. Then is goes back to master.

I’m unsure that I’m using the right Git commands, as Git will give me this message whenever I perform a git checkout SHA:

  • Branching strategy for configurable software
  • How git branches and tags are stored in disks?
  • restore deleted git branch you don't know the name of
  • git command to emit name of remote tracking branch
  • Efficiency of branching in shaders
  • git checkout '…' always results in error: pathspec '…' did not match any file(s) known to git
  • You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.
    
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:
    
    git checkout -b new_branch_name
    

    The only thing I want to do is to reset my working directory to a specific commit, upload those files, and go back to HEAD/master. I might make a few changes to files when I go to the specific commit SHA (convert XML to JSON or something), but I just want to loose all of those changes when I go back to master. Right now this is my code

    git checkout SHA
    
    # do a bunch of conversion and uploading
    
    git checkout master
    

    Is that the preferred way of doing what I want? Will I always be able to do pulls from the origin without getting any file conflicts (I don’t want to keep whatever I’m doing between the checkouts)?

    I’m asking because I’m seeing “your master and origin/master diverged” some times, although I’m not sure it’s caused by this.

    Thanks in advance.

  • gitignore across all branches?
  • How to create releases for public or private repository in GitHub?
  • GIT Rebase a Branch that is collaborated on?
  • Complexity of merging storyboard file
  • How to get rid of “merge branch…” automatic messages in git repository
  • What makes some version control systems better at merging?
  • 5 Solutions collect form web for “Git checkout commit, do stuff, and go back to master”

    It’s perfectly OK to do this. When HEAD isn’t corresponding to a branch name it will be shown as a detached HEAD. Nothing wrong with that.

    You can also use git show SHA1:relative/path to take a snapshot of file at specific point.

    You mentioned pulling changes from the origin. In your case you may also use git reset --hard SHA1 to go back to a specific commit and then git pull the missing changes from origin/master.

    For what you describe, you can just do something like:

    git archive -o /some/where/archive.tar.gz –prefix=<something> <commit> <file list>

    Look at the manual for git archive for the gory details. You can create zip files, tar files (compressed or not), opionally place a prefix “above” the packed files, all taken from the referenced commit.

    This is what I use to publish API, test and code coverage reports back up to my github page (gh-pages branch) after a successful Bamboo build. Its an Ant target that does the kind of thing you describe. (Lots of good answers here, but posting in case it helps).

    <target name="--publish.site">
        <echo file="${temp.dir}/publish-site.sh">
            #!/bin/sh
            cd ${basedir}
            cp -fr ${basedir}/schema ${reports.dir}
            git remote set-url origin git@github.com:jasperblues/my-project.git
            git fetch origin gh-pages:gh-pages
            git checkout gh-pages
            git pull
            rm -fr ./coverage
            cp -fr ${reports.dir}/coverage/ ./coverage
            git add ./coverage
            rm -fr ./api
            cp -fr ${reports.dir}/api ./api
            git add api
            cp -fr ${reports.dir}/schema ./schema
            git add schema
            git commit -a -m "publish reports to gh-pages"
            git push -u origin gh-pages
            git checkout master
        </echo>
        <chmod perm="+x" file="${temp.dir}/publish-site.sh"/>
        <exec executable="${temp.dir}/publish-site.sh" failonerror="true" failifexecutionfails="true">
            <env key="PATH" value="${tools.paths}"/>
        </exec>
    </target>
    
    Git Baby is a git and github fan, let's start git clone.