How do I push a new local branch to a remote Git repository and track it too?

后端 未结 15 1608
逝去的感伤
逝去的感伤 2020-11-22 09:17

I want to be able to do the following:

  1. Create a local branch based on some other (remote or local) branch (via git branch or git checkout

15条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:00

    For greatest flexibility, you could use a custom Git command. For example, create the following Python script somewhere in your $PATH under the name git-publish and make it executable:

    #!/usr/bin/env python3
    
    import argparse
    import subprocess
    import sys
    
    
    def publish(args):
        return subprocess.run(['git', 'push', '--set-upstream', args.remote, args.branch]).returncode
    
    
    def parse_args():
        parser = argparse.ArgumentParser(description='Push and set upstream for a branch')
        parser.add_argument('-r', '--remote', default='origin',
                            help="The remote name (default is 'origin')")
        parser.add_argument('-b', '--branch', help='The branch name (default is whatever HEAD is pointing to)',
                            default='HEAD')
        return parser.parse_args()
    
    
    def main():
        args = parse_args()
        return publish(args)
    
    
    if __name__ == '__main__':
        sys.exit(main())
    

    Then git publish -h will show you usage information:

    usage: git-publish [-h] [-r REMOTE] [-b BRANCH]
    
    Push and set upstream for a branch
    
    optional arguments:
      -h, --help            show this help message and exit
      -r REMOTE, --remote REMOTE
                            The remote name (default is 'origin')
      -b BRANCH, --branch BRANCH
                            The branch name (default is whatever HEAD is pointing to)
    

提交回复
热议问题